Spaces:
eaglesarezzo
/
Running on Zero

eaglesarezzo commited on
Commit
3f057a2
โ€ข
1 Parent(s): e67fd61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -36
app.py CHANGED
@@ -24,15 +24,6 @@ backend = Backend()
24
 
25
  cv2.setNumThreads(1)
26
 
27
- def load_topic_data(topic):
28
- if topic:
29
- query_engine = backend.create_index_for_query_engine(documents_paths[topic])
30
- gr.Info(f"Data loaded for {topic} topic")
31
- return query_engine
32
- else:
33
- gr.Warning("No topic selected. Please select a topic first.")
34
- return None
35
-
36
  @spaces.GPU(duration=20)
37
  def respond(
38
  message,
@@ -44,14 +35,21 @@ def respond(
44
  top_p,
45
  top_k,
46
  repeat_penalty,
47
- query_engine
48
  ):
49
  chat_template = MessagesFormatterType.GEMMA_2
50
 
51
- if query_engine is None:
52
- return history + [[message, "Please select a topic before asking questions."]]
53
 
54
- full_prompt = backend.generate_prompt(query_engine, message)
 
 
 
 
 
 
 
55
 
56
  # Load model only if it's not already loaded or if a new model is selected
57
  if backend.llm is None or backend.llm_model != model:
@@ -96,21 +94,12 @@ def respond(
96
  outputs = ""
97
  for output in stream:
98
  outputs += output
99
- yield history + [[message, outputs]]
100
  except Exception as e:
101
  yield history + [[message, f"Error during response generation: {str(e)}"]]
102
 
103
  def select_topic(topic):
104
- query_engine = load_topic_data(topic)
105
- return (
106
- gr.update(interactive=True), # Enable the chat input
107
- gr.update(interactive=True), # Enable the submit button
108
- gr.update(visible=True), # Make the chatbot visible
109
- gr.update(interactive=False), # Disable blockchain button
110
- gr.update(interactive=False), # Disable metaverse button
111
- gr.update(interactive=False), # Disable payment button
112
- query_engine # Return the loaded query engine
113
- )
114
 
115
  with gr.Blocks(css="""
116
  .gradio-container {
@@ -125,7 +114,7 @@ with gr.Blocks(css="""
125
  metaverse_btn = gr.Button("๐ŸŒ Metaverse", scale=1)
126
  payment_btn = gr.Button("๐Ÿ’ณ Payment", scale=1)
127
 
128
- query_engine = gr.State(None)
129
 
130
  chatbot = gr.Chatbot(
131
  scale=1,
@@ -140,9 +129,8 @@ with gr.Blocks(css="""
140
  show_label=False,
141
  placeholder="Inserisci il tuo messaggio...",
142
  container=False,
143
- interactive=False
144
  )
145
- submit_btn = gr.Button("Invia", scale=1, interactive=False)
146
 
147
  with gr.Accordion("Advanced Options", open=False):
148
  model = gr.Dropdown([
@@ -158,29 +146,26 @@ with gr.Blocks(css="""
158
  value="gemma-2-2b-it-Q6_K_L.gguf",
159
  label="Model"
160
  )
161
- 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.
162
- Nel fornire la risposta cita il report da cui la hai ottenuta.
163
- Utilizza la cronologia della chat o il contesto fornito per aiutare l'utente a ottenere una risposta accurata.
164
- Non rispondere mai a domande che non sono pertinenti a questi argomenti.""", label="System message")
165
  max_tokens = gr.Slider(minimum=1, maximum=4096, value=3048, step=1, label="Max tokens")
166
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature")
167
  top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
168
  top_k = gr.Slider(minimum=0, maximum=100, value=30, step=1, label="Top-k")
169
  repeat_penalty = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
170
 
171
- blockchain_btn.click(lambda: select_topic("blockchain"), inputs=None, outputs=[msg, submit_btn, chatbot, blockchain_btn, metaverse_btn, payment_btn, query_engine])
172
- metaverse_btn.click(lambda: select_topic("metaverse"), inputs=None, outputs=[msg, submit_btn, chatbot, blockchain_btn, metaverse_btn, payment_btn, query_engine])
173
- payment_btn.click(lambda: select_topic("payment"), inputs=None, outputs=[msg, submit_btn, chatbot, blockchain_btn, metaverse_btn, payment_btn, query_engine])
174
 
175
  submit_btn.click(
176
  respond,
177
- inputs=[msg, chatbot, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, query_engine],
178
  outputs=chatbot
179
  )
180
 
181
  msg.submit(
182
  respond,
183
- inputs=[msg, chatbot, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, query_engine],
184
  outputs=chatbot
185
  )
186
 
 
24
 
25
  cv2.setNumThreads(1)
26
 
 
 
 
 
 
 
 
 
 
27
  @spaces.GPU(duration=20)
28
  def respond(
29
  message,
 
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
+ full_prompt = 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
+ full_prompt = 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
55
  if backend.llm is None or backend.llm_model != model:
 
94
  outputs = ""
95
  for output in stream:
96
  outputs += output
97
+ yield history + [[message, outputs]] # Use original message, not full_prompt
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
 
104
  with gr.Blocks(css="""
105
  .gradio-container {
 
114
  metaverse_btn = gr.Button("๐ŸŒ Metaverse", scale=1)
115
  payment_btn = gr.Button("๐Ÿ’ณ Payment", scale=1)
116
 
117
+ selected_topic = gr.State(value="")
118
 
119
  chatbot = gr.Chatbot(
120
  scale=1,
 
129
  show_label=False,
130
  placeholder="Inserisci il tuo messaggio...",
131
  container=False,
 
132
  )
133
+ submit_btn = gr.Button("Invia", scale=1)
134
 
135
  with gr.Accordion("Advanced Options", open=False):
136
  model = gr.Dropdown([
 
146
  value="gemma-2-2b-it-Q6_K_L.gguf",
147
  label="Model"
148
  )
149
+ system_message = gr.Textbox(value=""" Inizia presentandoti come "Odi", un assistente ricercatore italiano sviluppato dagli Osservatori del Politecnico di Milano, specializzato in innovazione digitale. Rispondi alle domande utilizzando esclusivamente i dati forniti tramite RAG. Se non trovi informazioni pertinenti, informa l'utente che non hai la risposta e suggerisci di contattare i responsabili dell'osservatorio, estraendo i loro nomi dai dati disponibili. Quando fornisci risposte, cita sempre il report specifico da cui hai ottenuto le informazioni. Utilizza la cronologia della chat e il contesto fornito per garantire risposte accurate e pertinenti. Non rispondere a domande fuori dal tuo ambito di competenza.""", label="System message")
 
 
 
150
  max_tokens = gr.Slider(minimum=1, maximum=4096, value=3048, step=1, label="Max tokens")
151
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=1.2, step=0.1, label="Temperature")
152
  top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
153
  top_k = gr.Slider(minimum=0, maximum=100, value=30, step=1, label="Top-k")
154
  repeat_penalty = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Repetition penalty")
155
 
156
+ blockchain_btn.click(lambda: select_topic("blockchain"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
157
+ metaverse_btn.click(lambda: select_topic("metaverse"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
158
+ payment_btn.click(lambda: select_topic("payment"), inputs=None, outputs=[chatbot, selected_topic, blockchain_btn, metaverse_btn, payment_btn])
159
 
160
  submit_btn.click(
161
  respond,
162
+ inputs=[msg, chatbot, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, selected_topic],
163
  outputs=chatbot
164
  )
165
 
166
  msg.submit(
167
  respond,
168
+ inputs=[msg, chatbot, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty, selected_topic],
169
  outputs=chatbot
170
  )
171