Carolzinha2010 commited on
Commit
313ba70
·
verified ·
1 Parent(s): 2687dba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -58
app.py CHANGED
@@ -5,16 +5,25 @@ import gradio as gr
5
  import requests
6
  import inspect
7
  import pandas as pd
 
 
8
 
9
  # Import SerpAPI
10
  from serpapi import GoogleSearch
11
 
 
 
 
 
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
  # Get SerpAPI key from environment variables
15
  SERPAPI_API_KEY = os.getenv('SERPAPI_API_KEY')
16
  print(f"SERPAPI_API_KEY (first 5 chars): {SERPAPI_API_KEY[:5] if SERPAPI_API_KEY else 'None'}...") # Debugging API key
17
 
 
 
 
18
 
19
  # --- Web Search Function (using SerpAPI) ---
20
  def web_search(query: str) -> list[dict]:
@@ -60,7 +69,7 @@ def web_search(query: str) -> list[dict]:
60
  else:
61
  print("No 'organic_results' key found in SerpAPI response.")
62
  # Print the whole response if no organic_results are found for debugging
63
- print(f"SerpAPI response (no organic results): {search_results_dict}")
64
 
65
 
66
  except Exception as e:
@@ -70,16 +79,17 @@ def web_search(query: str) -> list[dict]:
70
  return results
71
 
72
 
73
- # --- Basic Agent Definition ---
74
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
75
-
76
  class BasicAgent:
77
 
78
  def __init__(self):
79
  print("BasicAgent initialized.")
80
- # Access the globally loaded model and tokenizer
81
- self.tokenizer = hf_tokenizer
82
- self.model = hf_model
 
 
 
83
 
84
 
85
  def __call__(self, question: str) -> str:
@@ -96,65 +106,88 @@ class BasicAgent:
96
  search_results = web_search(question) # Call the web_search function
97
  print(f"Received {len(search_results)} search results from web_search.") # Debugging results received
98
 
99
- if search_results:
100
- # Format search results into a context string for the LLM
 
 
 
101
  context = ""
102
- for i, result in enumerate(search_results[:3]): # Use top 3 results
103
- context += f"Result {i+1}:\n"
104
  if result.get('title'):
105
- context += f"Title: {result['title']}\n"
106
  if result.get('snippet'):
107
- context += f"Snippet: {result['snippet']}\n"
108
  if result.get('url'):
109
- context += f"URL: {result['url']}\n"
110
- context += "---\n"
 
 
 
 
 
 
 
111
 
112
- # Construct the prompt for the LLM
113
- prompt = f"Question: {question}\n\nSearch Results:\n{context}\nBased on the search results provided, please answer the question."
114
- print(f"LLM Prompt (first 200 chars): {prompt[:200]}...") # Debugging prompt
 
 
 
115
 
116
  try:
117
- # Encode the prompt
118
- inputs = self.tokenizer(prompt, return_tensors="pt")
119
-
120
- # Generate response using the LLM
121
- # Adjust generation parameters as needed
122
- output_sequences = self.model.generate(
123
- **inputs,
124
- max_length=512, # Maximum length of the generated text
125
- num_return_sequences=1, # Number of sequences to generate
126
- no_repeat_ngram_size=2, # Avoid repeating n-grams
127
- do_sample=True, # Enable sampling
128
- top_k=50, # Sample from top_k tokens
129
- top_p=0.95, # Sample from top_p probability mass
130
- temperature=0.7, # Control randomness
131
- attention_mask=inputs['attention_mask'] # Pass attention mask
132
- )
133
-
134
- # Decode the generated output
135
- generated_text = self.tokenizer.decode(output_sequences[0], skip_special_tokens=True)
136
- print(f"LLM Generated Text (first 200 chars): {generated_text[:200]}...") # Debugging generated text
137
-
138
- # Extract the answer from the generated text
139
- # For CausalLMs like gpt2, the prompt is included in the output,
140
- # so we need to remove it.
141
- if generated_text.startswith(prompt):
142
- llm_answer = generated_text[len(prompt):].strip()
143
  else:
144
- # Fallback if the output format is unexpected
145
- llm_answer = generated_text.strip()
 
 
146
 
147
  if llm_answer:
148
- print(f"Agent returning LLM-based answer: {llm_answer[:100]}...")
149
  return llm_answer
150
  else:
151
- print("LLM generated empty or whitespace answer.")
152
- return "I couldn't generate a specific answer based on the search results."
153
-
154
-
155
- except Exception as e:
156
- print(f"Error during LLM generation: {e}")
157
- return f"An error occurred while generating the answer using the LLM: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
 
160
  else:
@@ -300,10 +333,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as
300
  **Instructions:**
301
  1. Ensure your agent logic is defined in the `BasicAgent` class above.
302
  2. **Get a SerpAPI key and add it as an environment variable in your runtime environment (e.g., as a secret in your Hugging Face Space settings).**
303
- 3. **Ensure your Hugging Face model and tokenizer are loaded (usually in a preceding cell).**
304
- 4. Log in to Hugging Face using the button below.
305
- 5. Click the "Run Evaluation & Submit All Answers" button.
306
- 6. The application will fetch questions, run your agent, submit answers, and display the results below.
307
  """
308
  )
309
  login_btn = gr.LoginButton()
 
5
  import requests
6
  import inspect
7
  import pandas as pd
8
+ import subprocess
9
+ import sys
10
 
11
  # Import SerpAPI
12
  from serpapi import GoogleSearch
13
 
14
+ # Import Hugging Face libraries
15
+ import torch
16
+ from transformers import AutoModelForCausalLM, AutoTokenizer
17
+
18
  # --- Constants ---
19
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
20
  # Get SerpAPI key from environment variables
21
  SERPAPI_API_KEY = os.getenv('SERPAPI_API_KEY')
22
  print(f"SERPAPI_API_KEY (first 5 chars): {SERPAPI_API_KEY[:5] if SERPAPI_API_KEY else 'None'}...") # Debugging API key
23
 
24
+ # Access the loaded Hugging Face model and tokenizer (loaded in a previous cell)
25
+ # Ensure these global variables are defined by running the model loading cell first.
26
+ global hf_model, hf_tokenizer
27
 
28
  # --- Web Search Function (using SerpAPI) ---
29
  def web_search(query: str) -> list[dict]:
 
69
  else:
70
  print("No 'organic_results' key found in SerpAPI response.")
71
  # Print the whole response if no organic_results are found for debugging
72
+ # print(f"SerpAPI response (no organic results): {search_results_dict}")
73
 
74
 
75
  except Exception as e:
 
79
  return results
80
 
81
 
82
+ # --- Basic Agent Definition (Updated to use LLM) ---
 
 
83
  class BasicAgent:
84
 
85
  def __init__(self):
86
  print("BasicAgent initialized.")
87
+ # Check if LLM and tokenizer are loaded (optional but good practice)
88
+ if 'hf_model' not in globals() or 'hf_tokenizer' not in globals():
89
+ print("Warning: Hugging Face model or tokenizer not loaded before agent initialization.")
90
+ # You might want to raise an error or handle this case more robustly
91
+ else:
92
+ print("Hugging Face model and tokenizer found.")
93
 
94
 
95
  def __call__(self, question: str) -> str:
 
106
  search_results = web_search(question) # Call the web_search function
107
  print(f"Received {len(search_results)} search results from web_search.") # Debugging results received
108
 
109
+ if search_results and hf_model and hf_tokenizer:
110
+ # --- Use LLM to process search results ---
111
+ print("Using LLM to process search results.")
112
+
113
+ # Format search results for the LLM
114
  context = ""
115
+ for i, result in enumerate(search_results[:5]): # Use top 5 results for context
116
+ context += f"Source {i+1}:\n"
117
  if result.get('title'):
118
+ context += f"Title: {result['title']}\n"
119
  if result.get('snippet'):
120
+ context += f"Snippet: {result['snippet']}\n"
121
  if result.get('url'):
122
+ context += f"URL: {result['url']}\n"
123
+ context += "---\n" # Separator
124
+
125
+ # Create a prompt for the LLM
126
+ prompt = f"""Using the following search results, answer the question accurately.
127
+ If the search results do not contain enough information to answer the question,
128
+ respond with "I couldn't find enough information in the search results."
129
+
130
+ Question: {question}
131
 
132
+ Search Results:
133
+ {context}
134
+
135
+ Answer:"""
136
+
137
+ print(f"LLM Prompt (first 500 chars):\n{prompt[:500]}...") # Debugging prompt
138
 
139
  try:
140
+ # Tokenize the prompt
141
+ inputs = hf_tokenizer(prompt, return_tensors="pt")
142
+
143
+ # Generate a response from the LLM
144
+ # Note: Generation parameters like max_length, temperature, do_sample
145
+ # can significantly affect the output.
146
+ # This is a basic example. You might need to experiment here.
147
+ # For gpt2, max_length might need adjustment if prompt+context is too long.
148
+ # Also, be aware of model context window limitations.
149
+ # If using a chat model, a chat template might be needed.
150
+ # For demonstration, using a simple generation approach.
151
+ generate_ids = hf_model.generate(inputs.input_ids, max_length=512, num_return_sequences=1, temperature=0.7, do_sample=True) # Adjust max_length as needed
152
+ generated_text = hf_tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]
153
+
154
+ # Extract only the answer part from the generated text if necessary
155
+ # Depending on the prompt and model, the model might repeat the prompt.
156
+ # A simple way is to look for the "Answer:" tag.
157
+ answer_tag = "Answer:"
158
+ if answer_tag in generated_text:
159
+ llm_answer = generated_text.split(answer_tag, 1)[1].strip()
 
 
 
 
 
 
160
  else:
161
+ llm_answer = generated_text.strip() # Fallback if tag not found
162
+
163
+ print(f"LLM generated text (first 100 chars): {generated_text[:100]}...") # Debugging raw output
164
+ print(f"Agent returning LLM-based answer (first 100 chars): {llm_answer[:100]}...") # Debugging final answer
165
 
166
  if llm_answer:
 
167
  return llm_answer
168
  else:
169
+ # Fallback if LLM generates empty response
170
+ print("LLM generated an empty response.")
171
+ return "I couldn't generate an answer based on the search results."
172
+
173
+ except Exception as llm_e:
174
+ print(f"An error occurred during LLM generation: {llm_e}")
175
+ return f"An error occurred while processing search results with the LLM: {llm_e}"
176
+
177
+ elif search_results:
178
+ # Fallback if model/tokenizer not loaded but search results exist
179
+ print("Hugging Face model or tokenizer not loaded. Cannot use LLM.")
180
+ # Return the old style answer if LLM is not available
181
+ answer_parts = []
182
+ for i, result in enumerate(search_results[:3]):
183
+ if result.get('snippet'):
184
+ answer_parts.append(f"Snippet {i+1}: {result['snippet']}")
185
+ elif result.get('title'):
186
+ answer_parts.append(f"Result {i+1} Title: {result['title']}")
187
+ if answer_parts:
188
+ return "Based on web search (LLM not available):\n" + "\n".join(answer_parts)
189
+ else:
190
+ return "I couldn't find useful information in the search results (LLM not available)."
191
 
192
 
193
  else:
 
333
  **Instructions:**
334
  1. Ensure your agent logic is defined in the `BasicAgent` class above.
335
  2. **Get a SerpAPI key and add it as an environment variable in your runtime environment (e.g., as a secret in your Hugging Face Space settings).**
336
+ 3. Log in to Hugging Face using the button below.
337
+ 4. Click the "Run Evaluation & Submit All Answers" button.
338
+ 5. The application will fetch questions, run your agent, submit answers, and display the results below.
 
339
  """
340
  )
341
  login_btn = gr.LoginButton()