GattoNero commited on
Commit
d4157c3
·
verified ·
1 Parent(s): c841c47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -78
app.py CHANGED
@@ -4,20 +4,7 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
7
- # (Keep Constants as is)
8
- # --- Constants ---
9
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
-
11
- # --- Basic Agent Definition ---
12
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
14
- def __init__(self):
15
- print("BasicAgent initialized.")
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -194,70 +181,38 @@ if __name__ == "__main__":
194
  demo.launch(debug=True, share=False)
195
 
196
 
197
- '''
198
- import gradio as gr
199
- from huggingface_hub import InferenceClient
200
-
201
- """
202
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
203
- """
204
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
205
-
206
-
207
- def respond(
208
- message,
209
- history: list[tuple[str, str]],
210
- system_message,
211
- max_tokens,
212
- temperature,
213
- top_p,
214
- ):
215
- messages = [{"role": "system", "content": system_message}]
216
-
217
- for val in history:
218
- if val[0]:
219
- messages.append({"role": "user", "content": val[0]})
220
- if val[1]:
221
- messages.append({"role": "assistant", "content": val[1]})
222
-
223
- messages.append({"role": "user", "content": message})
224
-
225
- response = ""
226
-
227
- for message in client.chat_completion(
228
- messages,
229
- max_tokens=max_tokens,
230
- stream=True,
231
- temperature=temperature,
232
- top_p=top_p,
233
- ):
234
- token = message.choices[0].delta.content
235
-
236
- response += token
237
- yield response
238
-
239
-
240
- """
241
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
242
- """
243
- demo = gr.ChatInterface(
244
- respond,
245
- additional_inputs=[
246
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
247
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
248
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
249
- gr.Slider(
250
- minimum=0.1,
251
- maximum=1.0,
252
- value=0.95,
253
- step=0.05,
254
- label="Top-p (nucleus sampling)",
255
- ),
256
- ],
257
- )
258
 
259
 
260
- if __name__ == "__main__":
261
- demo.launch()
 
262
 
263
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import inspect
5
  import pandas as pd
6
 
7
+ ##Roba per la valutazione
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def run_and_submit_all( profile: gr.OAuthProfile | None):
10
  """
 
181
  demo.launch(debug=True, share=False)
182
 
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
 
186
+ # (Keep Constants as is)
187
+ # --- Constants ---
188
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
189
 
190
+ # --- Basic Agent Definition ---
191
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
192
+ class BasicAgent:
193
+ print("Initializing LlamaIndex-based agent...")
194
+
195
+ # Imposta l'LLM (puoi usare anche altri modelli via HuggingFace o OpenRouter)
196
+ self.llm = HfApiModel()
197
+
198
+ #OpenAI(model="gpt-3.5-turbo", temperature=0)
199
+
200
+ # Crea un ServiceContext con il tuo LLM
201
+ self.service_context = ServiceContext.from_defaults(llm=self.llm)
202
+
203
+ # Carica i documenti dalla directory "data/"
204
+ self.documents = SimpleDirectoryReader("data").load_data()
205
+
206
+ # Crea un indice vettoriale
207
+ self.index = VectorStoreIndex.from_documents(
208
+ self.documents, service_context=self.service_context
209
+ )
210
+
211
+ # Crea il query engine
212
+ self.query_engine = self.index.as_query_engine()
213
+
214
+ def __call__(self, question: str) -> str:
215
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
216
+ response = self.query_engine.query(question)
217
+ print(f"Agent returning response: {response}")
218
+ return str(response)