Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -10,14 +11,47 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -40,7 +74,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from transformers import pipeline
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
|
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
+
# class BasicAgent:
|
| 15 |
+
# def __init__(self):
|
| 16 |
+
# print("BasicAgent initialized.")
|
| 17 |
+
# def __call__(self, question: str) -> str:
|
| 18 |
+
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 19 |
+
# fixed_answer = "This is a default answer."
|
| 20 |
+
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 21 |
+
# return fixed_answer
|
| 22 |
+
class SmartAgent:
|
| 23 |
def __init__(self):
|
| 24 |
+
self.generator = pipeline(
|
| 25 |
+
"text-generation",
|
| 26 |
+
model="mistralai/Mistral-7B-Instruct-v0.1",
|
| 27 |
+
max_new_tokens=256,
|
| 28 |
+
temperature=0.3,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
device_map="auto"
|
| 31 |
+
)
|
| 32 |
+
self.system_prompt = (
|
| 33 |
+
"You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer "
|
| 34 |
+
"with the following template: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR "
|
| 35 |
+
"as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, "
|
| 36 |
+
"don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
|
| 37 |
+
"If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the "
|
| 38 |
+
"digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
def extract_final_answer(self, text: str) -> str:
|
| 42 |
+
# Извлекаем только ответ без "FINAL ANSWER:"
|
| 43 |
+
if "FINAL ANSWER:" in text:
|
| 44 |
+
return text.split("FINAL ANSWER:")[-1].strip().split("\n")[0]
|
| 45 |
+
return text.strip()
|
| 46 |
+
|
| 47 |
def __call__(self, question: str) -> str:
|
| 48 |
+
prompt = f"{self.system_prompt}\nQuestion: {question}\n"
|
| 49 |
+
output = self.generator(prompt, return_full_text=False)[0]['generated_text']
|
| 50 |
+
answer = self.extract_final_answer(output)
|
| 51 |
+
print(f"[DEBUG] Question: {question}")
|
| 52 |
+
print(f"[DEBUG] Output: {output}")
|
| 53 |
+
print(f"[DEBUG] Answer: {answer}")
|
| 54 |
+
return answer
|
| 55 |
|
| 56 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 57 |
"""
|
|
|
|
| 74 |
|
| 75 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 76 |
try:
|
| 77 |
+
agent = SmartAgent()
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error instantiating agent: {e}")
|
| 80 |
return f"Error initializing agent: {e}", None
|