Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from llama_cpp_agent.chat_history import BasicChatHistory
|
|
| 9 |
from llama_cpp_agent.chat_history.messages import Roles
|
| 10 |
import cv2
|
| 11 |
|
| 12 |
-
#
|
| 13 |
huggingface_token = os.environ.get('HF_TOKEN')
|
| 14 |
download_models(huggingface_token)
|
| 15 |
|
|
@@ -19,8 +19,9 @@ documents_paths = {
|
|
| 19 |
'payment': 'data/payment'
|
| 20 |
}
|
| 21 |
|
| 22 |
-
#
|
| 23 |
backend = Backend()
|
|
|
|
| 24 |
cv2.setNumThreads(1)
|
| 25 |
|
| 26 |
@spaces.GPU(duration=20)
|
|
@@ -36,8 +37,7 @@ def respond(
|
|
| 36 |
repeat_penalty,
|
| 37 |
selected_topic
|
| 38 |
):
|
| 39 |
-
|
| 40 |
-
chat_template = MessagesFormatterType.SIMPLE # Change to a valid type if DEFAULT is not available
|
| 41 |
|
| 42 |
print("HISTORY SO FAR ", history)
|
| 43 |
print("Selected topic:", selected_topic)
|
|
@@ -77,7 +77,7 @@ def respond(
|
|
| 77 |
|
| 78 |
messages = BasicChatHistory()
|
| 79 |
|
| 80 |
-
#
|
| 81 |
for user_msg, assistant_msg in history:
|
| 82 |
messages.add_message({'role': Roles.user, 'content': user_msg})
|
| 83 |
messages.add_message({'role': Roles.assistant, 'content': assistant_msg})
|
|
@@ -98,9 +98,6 @@ def respond(
|
|
| 98 |
except Exception as e:
|
| 99 |
yield history + [[message, f"Error during response generation: {str(e)}"]]
|
| 100 |
|
| 101 |
-
def reset_conversation():
|
| 102 |
-
return [], "", gr.update(visible=False), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
|
| 103 |
-
|
| 104 |
def select_topic(topic):
|
| 105 |
return gr.update(visible=True), topic, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
|
| 106 |
|
|
@@ -116,7 +113,6 @@ with gr.Blocks(css="""
|
|
| 116 |
blockchain_btn = gr.Button("π Blockchain", scale=1)
|
| 117 |
metaverse_btn = gr.Button("π Metaverse", scale=1)
|
| 118 |
payment_btn = gr.Button("π³ Payment", scale=1)
|
| 119 |
-
reset_btn = gr.Button("π Reset", scale=1) # Reset button
|
| 120 |
|
| 121 |
selected_topic = gr.State(value="")
|
| 122 |
|
|
@@ -150,7 +146,10 @@ with gr.Blocks(css="""
|
|
| 150 |
value="gemma-2-2b-it-Q6_K_L.gguf",
|
| 151 |
label="Model"
|
| 152 |
)
|
| 153 |
-
system_message = gr.Textbox(value="""
|
|
|
|
|
|
|
|
|
|
| 154 |
max_tokens = gr.Slider(minimum=1, maximum=4096, value=3048, step=1, label="Max tokens")
|
| 155 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature")
|
| 156 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
|
@@ -160,8 +159,6 @@ with gr.Blocks(css="""
|
|
| 160 |
blockchain_btn.click(lambda: select_topic("blockchain"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
|
| 161 |
metaverse_btn.click(lambda: select_topic("metaverse"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
|
| 162 |
payment_btn.click(lambda: select_topic("payment"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
|
| 163 |
-
|
| 164 |
-
reset_btn.click(reset_conversation, inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn]) # Connect reset button
|
| 165 |
|
| 166 |
submit_btn.click(
|
| 167 |
respond,
|
|
|
|
| 9 |
from llama_cpp_agent.chat_history.messages import Roles
|
| 10 |
import cv2
|
| 11 |
|
| 12 |
+
# get the models
|
| 13 |
huggingface_token = os.environ.get('HF_TOKEN')
|
| 14 |
download_models(huggingface_token)
|
| 15 |
|
|
|
|
| 19 |
'payment': 'data/payment'
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# initialize backend
|
| 23 |
backend = Backend()
|
| 24 |
+
|
| 25 |
cv2.setNumThreads(1)
|
| 26 |
|
| 27 |
@spaces.GPU(duration=20)
|
|
|
|
| 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)
|
|
|
|
| 77 |
|
| 78 |
messages = BasicChatHistory()
|
| 79 |
|
| 80 |
+
# add user and assistant messages to the history
|
| 81 |
for user_msg, assistant_msg in history:
|
| 82 |
messages.add_message({'role': Roles.user, 'content': user_msg})
|
| 83 |
messages.add_message({'role': Roles.assistant, 'content': assistant_msg})
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
yield history + [[message, f"Error during response generation: {str(e)}"]]
|
| 100 |
|
|
|
|
|
|
|
|
|
|
| 101 |
def select_topic(topic):
|
| 102 |
return gr.update(visible=True), topic, gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
|
| 103 |
|
|
|
|
| 113 |
blockchain_btn = gr.Button("π Blockchain", scale=1)
|
| 114 |
metaverse_btn = gr.Button("π Metaverse", scale=1)
|
| 115 |
payment_btn = gr.Button("π³ Payment", scale=1)
|
|
|
|
| 116 |
|
| 117 |
selected_topic = gr.State(value="")
|
| 118 |
|
|
|
|
| 146 |
value="gemma-2-2b-it-Q6_K_L.gguf",
|
| 147 |
label="Model"
|
| 148 |
)
|
| 149 |
+
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.
|
| 150 |
+
Nel fornire la risposta cita il report da cui la hai ottenuta.
|
| 151 |
+
Utilizza la cronologia della chat o il contesto fornito per aiutare l'utente a ottenere una risposta accurata.
|
| 152 |
+
Non rispondere mai a domande che non sono pertinenti a questi argomenti.""", label="System message")
|
| 153 |
max_tokens = gr.Slider(minimum=1, maximum=4096, value=3048, step=1, label="Max tokens")
|
| 154 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature")
|
| 155 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
|
|
|
| 159 |
blockchain_btn.click(lambda: select_topic("blockchain"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
|
| 160 |
metaverse_btn.click(lambda: select_topic("metaverse"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
|
| 161 |
payment_btn.click(lambda: select_topic("payment"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
|
|
|
|
|
|
|
| 162 |
|
| 163 |
submit_btn.click(
|
| 164 |
respond,
|