Spaces:
Build error
Build error
Create app.py
Browse filesfinally that have chances of going right
app.py
CHANGED
|
@@ -77,6 +77,10 @@ class BasicAgent:
|
|
| 77 |
|
| 78 |
def __init__(self):
|
| 79 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
def __call__(self, question: str) -> str:
|
| 82 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
@@ -93,27 +97,65 @@ class BasicAgent:
|
|
| 93 |
print(f"Received {len(search_results)} search results from web_search.") # Debugging results received
|
| 94 |
|
| 95 |
if search_results:
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
for i, result in enumerate(search_results[:3]): # Use top 3 results
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
if result.get('snippet'):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
print(f"
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
else:
|
| 119 |
print("Web search returned no results.")
|
|
@@ -258,9 +300,10 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Basic Agent Evaluation Runner") as
|
|
| 258 |
**Instructions:**
|
| 259 |
1. Ensure your agent logic is defined in the `BasicAgent` class above.
|
| 260 |
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).**
|
| 261 |
-
3.
|
| 262 |
-
4.
|
| 263 |
-
5.
|
|
|
|
| 264 |
"""
|
| 265 |
)
|
| 266 |
login_btn = gr.LoginButton()
|
|
|
|
| 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:
|
| 86 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 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:
|
| 161 |
print("Web search returned no results.")
|
|
|
|
| 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()
|