Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,8 +19,7 @@ documents_paths = {
|
|
| 19 |
'payment': 'data/payment'
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
| 23 |
-
# initialize backend (not ideal as global variable...)
|
| 24 |
backend = Backend()
|
| 25 |
|
| 26 |
cv2.setNumThreads(1)
|
|
@@ -36,33 +35,20 @@ def respond(
|
|
| 36 |
top_p,
|
| 37 |
top_k,
|
| 38 |
repeat_penalty,
|
|
|
|
| 39 |
):
|
| 40 |
chat_template = MessagesFormatterType.GEMMA_2
|
| 41 |
|
| 42 |
print("HISTORY SO FAR ", history)
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
matched_path = path
|
| 50 |
-
break
|
| 51 |
-
print("matched_path", matched_path)
|
| 52 |
-
|
| 53 |
-
if matched_path: # this case would only be true in second interaction
|
| 54 |
-
original_message = history[0][0]
|
| 55 |
-
print("** matched path!!")
|
| 56 |
-
query_engine = backend.create_index_for_query_engine(matched_path)
|
| 57 |
-
message = backend.generate_prompt(query_engine, original_message)
|
| 58 |
-
|
| 59 |
-
gr.Info("Relevant context indexed from docs...")
|
| 60 |
-
|
| 61 |
-
elif (not matched_path) and (len(history) > 1):
|
| 62 |
-
print("Using context from storage db")
|
| 63 |
query_engine = backend.load_index_for_query_engine()
|
| 64 |
message = backend.generate_prompt(query_engine, message)
|
| 65 |
-
|
| 66 |
gr.Info("Relevant context extracted from db...")
|
| 67 |
|
| 68 |
# Load model only if it's not already loaded or if a new model is selected
|
|
@@ -98,7 +84,6 @@ def respond(
|
|
| 98 |
messages.add_message(user)
|
| 99 |
messages.add_message(assistant)
|
| 100 |
|
| 101 |
-
|
| 102 |
try:
|
| 103 |
stream = agent.get_chat_response(
|
| 104 |
message,
|
|
@@ -114,73 +99,80 @@ def respond(
|
|
| 114 |
yield outputs
|
| 115 |
except Exception as e:
|
| 116 |
yield f"Error during response generation: {str(e)}"
|
| 117 |
-
|
| 118 |
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
fn=respond,
|
| 122 |
-
css="""
|
| 123 |
.gradio-container {
|
| 124 |
background-color: #B9D9EB;
|
| 125 |
color: #003366;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
value="gemma-2-2b-it-Q6_K_L.gguf",
|
| 139 |
-
label="Model"
|
| 140 |
-
),
|
| 141 |
-
gr.Textbox(value="""Solamente all'inizio, presentati come Odi, un assistente ricercatore italiano creato dagli Osservatori del Politecnico di Milano e specializzato nel fornire risposte precise e pertinenti solo ad argomenti di innovazione digitale.
|
| 142 |
-
Solo nella tua prima risposta, se non è chiaro, chiedi all'utente di indicare a quale di queste tre sezioni degli Osservatori si riferisce la sua domanda: 'Blockchain', 'Payment' o 'Metaverse'. Nel fornire la risposta cita il report da cui la hai ottenuta.
|
| 143 |
-
Per le risposte successive, utilizza la cronologia della chat o il contesto fornito per aiutare l'utente a ottenere una risposta accurata.
|
| 144 |
-
Non rispondere mai a domande che non sono pertinenti a questi argomenti.""", label="System message"),
|
| 145 |
-
gr.Slider(minimum=1, maximum=4096, value=3048, step=1, label="Max tokens"),
|
| 146 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature"),
|
| 147 |
-
gr.Slider(
|
| 148 |
-
minimum=0.1,
|
| 149 |
-
maximum=1.0,
|
| 150 |
-
value=0.95,
|
| 151 |
-
step=0.05,
|
| 152 |
-
label="Top-p",
|
| 153 |
-
),
|
| 154 |
-
gr.Slider(
|
| 155 |
-
minimum=0,
|
| 156 |
-
maximum=100,
|
| 157 |
-
value=30,
|
| 158 |
-
step=1,
|
| 159 |
-
label="Top-k",
|
| 160 |
-
),
|
| 161 |
-
gr.Slider(
|
| 162 |
-
minimum=0.0,
|
| 163 |
-
maximum=2.0,
|
| 164 |
-
value=1.1,
|
| 165 |
-
step=0.1,
|
| 166 |
-
label="Repetition penalty",
|
| 167 |
-
|
| 168 |
-
),
|
| 169 |
-
|
| 170 |
-
],
|
| 171 |
-
retry_btn="Riprova",
|
| 172 |
-
undo_btn="Annulla",
|
| 173 |
-
clear_btn="Riavvia chat",
|
| 174 |
-
submit_btn="Invia",
|
| 175 |
-
title="Odi, l'assistente ricercatore degli Osservatori",
|
| 176 |
-
chatbot=gr.Chatbot(
|
| 177 |
scale=1,
|
| 178 |
likeable=False,
|
| 179 |
-
show_copy_button=True
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
-
demo.launch()
|
|
|
|
| 19 |
'payment': 'data/payment'
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# initialize backend
|
|
|
|
| 23 |
backend = Backend()
|
| 24 |
|
| 25 |
cv2.setNumThreads(1)
|
|
|
|
| 35 |
top_p,
|
| 36 |
top_k,
|
| 37 |
repeat_penalty,
|
| 38 |
+
selected_topic
|
| 39 |
):
|
| 40 |
chat_template = MessagesFormatterType.GEMMA_2
|
| 41 |
|
| 42 |
print("HISTORY SO FAR ", history)
|
| 43 |
+
print("Selected topic:", selected_topic)
|
| 44 |
|
| 45 |
+
if selected_topic:
|
| 46 |
+
query_engine = backend.create_index_for_query_engine(documents_paths[selected_topic])
|
| 47 |
+
message = backend.generate_prompt(query_engine, message)
|
| 48 |
+
gr.Info(f"Relevant context indexed from {selected_topic} docs...")
|
| 49 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
query_engine = backend.load_index_for_query_engine()
|
| 51 |
message = backend.generate_prompt(query_engine, message)
|
|
|
|
| 52 |
gr.Info("Relevant context extracted from db...")
|
| 53 |
|
| 54 |
# Load model only if it's not already loaded or if a new model is selected
|
|
|
|
| 84 |
messages.add_message(user)
|
| 85 |
messages.add_message(assistant)
|
| 86 |
|
|
|
|
| 87 |
try:
|
| 88 |
stream = agent.get_chat_response(
|
| 89 |
message,
|
|
|
|
| 99 |
yield outputs
|
| 100 |
except Exception as e:
|
| 101 |
yield f"Error during response generation: {str(e)}"
|
|
|
|
| 102 |
|
| 103 |
+
def select_topic(topic):
|
| 104 |
+
return gr.update(visible=True), gr.update(value=topic)
|
| 105 |
|
| 106 |
+
with gr.Blocks(css="""
|
|
|
|
|
|
|
| 107 |
.gradio-container {
|
| 108 |
background-color: #B9D9EB;
|
| 109 |
color: #003366;
|
| 110 |
+
}
|
| 111 |
+
""") as demo:
|
| 112 |
+
gr.Markdown("# Odi, l'assistente ricercatore degli Osservatori")
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
blockchain_btn = gr.Button("🔗 Blockchain", scale=1)
|
| 116 |
+
metaverse_btn = gr.Button("🌐 Metaverse", scale=1)
|
| 117 |
+
payment_btn = gr.Button("💳 Payment", scale=1)
|
| 118 |
+
|
| 119 |
+
selected_topic = gr.State(value="")
|
| 120 |
+
|
| 121 |
+
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
scale=1,
|
| 123 |
likeable=False,
|
| 124 |
+
show_copy_button=True,
|
| 125 |
+
visible=False
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
msg = gr.Textbox(
|
| 130 |
+
scale=4,
|
| 131 |
+
show_label=False,
|
| 132 |
+
placeholder="Inserisci il tuo messaggio...",
|
| 133 |
+
container=False,
|
| 134 |
+
)
|
| 135 |
+
submit_btn = gr.Button("Invia", scale=1)
|
| 136 |
+
|
| 137 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 138 |
+
model = gr.Dropdown([
|
| 139 |
+
'Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf',
|
| 140 |
+
'Mistral-Nemo-Instruct-2407-Q5_K_M.gguf',
|
| 141 |
+
'gemma-2-2b-it-Q6_K_L.gguf',
|
| 142 |
+
'openchat-3.6-8b-20240522-Q6_K.gguf',
|
| 143 |
+
'Llama-3-Groq-8B-Tool-Use-Q6_K.gguf',
|
| 144 |
+
'MiniCPM-V-2_6-Q6_K.gguf',
|
| 145 |
+
'llama-3.1-storm-8b-q5_k_m.gguf',
|
| 146 |
+
'orca-2-7b-patent-instruct-llama-2-q5_k_m.gguf'
|
| 147 |
+
],
|
| 148 |
+
value="gemma-2-2b-it-Q6_K_L.gguf",
|
| 149 |
+
label="Model"
|
| 150 |
+
)
|
| 151 |
+
system_message = gr.Textbox(value="""Solamente all'inizio, presentati come Odi, un assistente ricercatore italiano creato dagli Osservatori del Politecnico di Milano e specializzato nel fornire risposte precise e pertinenti solo ad argomenti di innovazione digitale.
|
| 152 |
+
Nel fornire la risposta cita il report da cui la hai ottenuta.
|
| 153 |
+
Utilizza la cronologia della chat o il contesto fornito per aiutare l'utente a ottenere una risposta accurata.
|
| 154 |
+
Non rispondere mai a domande che non sono pertinenti a questi argomenti.""", label="System message")
|
| 155 |
+
max_tokens = gr.Slider(minimum=1, maximum=4096, value=3048, step=1, label="Max tokens")
|
| 156 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature")
|
| 157 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 158 |
+
top_k = gr.Slider(minimum=0, maximum=100, value=30, step=1, label="Top-k")
|
| 159 |
+
repeat_penalty = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
|
| 160 |
+
|
| 161 |
+
blockchain_btn.click(select_topic, inputs="blockchain", outputs=[chatbot, selected_topic])
|
| 162 |
+
metaverse_btn.click(select_topic, inputs="metaverse", outputs=[chatbot, selected_topic])
|
| 163 |
+
payment_btn.click(select_topic, inputs="payment", outputs=[chatbot, selected_topic])
|
| 164 |
+
|
| 165 |
+
submit_btn.click(
|
| 166 |
+
respond,
|
| 167 |
+
inputs=[msg, chatbot, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, selected_topic],
|
| 168 |
+
outputs=chatbot
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
msg.submit(
|
| 172 |
+
respond,
|
| 173 |
+
inputs=[msg, chatbot, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, selected_topic],
|
| 174 |
+
outputs=chatbot
|
| 175 |
+
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
+
demo.launch()
|