Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,13 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
-
from google import genai
|
7 |
-
from google.genai import types
|
|
|
|
|
|
|
|
|
|
|
8 |
# (Keep Constants as is)
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -20,49 +25,83 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
20 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
21 |
# return fixed_answer
|
22 |
|
23 |
-
class BasicAgent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def __init__(self):
|
25 |
-
print("
|
26 |
-
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
# Initialize the Gemini client
|
35 |
-
self.client = genai.Client()
|
36 |
-
|
37 |
-
# Set model ID (you can choose another if needed)
|
38 |
-
self.model_id = "gemini-2.0-flash-exp"
|
39 |
-
|
40 |
-
# (Optional) Define generation config
|
41 |
-
self.generation_config = types.GenerateContentConfig(
|
42 |
-
temperature=0.4,
|
43 |
-
top_p=0.95,
|
44 |
-
top_k=20,
|
45 |
-
candidate_count=1,
|
46 |
-
seed=5,
|
47 |
-
presence_penalty=0.0,
|
48 |
-
frequency_penalty=0.0,
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
)
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
print(f"Error during Gemini API call: {str(e)}")
|
65 |
-
return f"Error: {str(e)}"
|
66 |
|
67 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
68 |
"""
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
# from google import genai
|
7 |
+
# from google.genai import types
|
8 |
+
import torch
|
9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
10 |
+
from smolagents.agents import ReActAgent
|
11 |
+
from smolagents.tools import tool
|
12 |
+
|
13 |
# (Keep Constants as is)
|
14 |
# --- Constants ---
|
15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
25 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
26 |
# return fixed_answer
|
27 |
|
28 |
+
# class BasicAgent:
|
29 |
+
# def __init__(self):
|
30 |
+
# print("CustomAgent (using Gemini 2.0) initialized.")
|
31 |
+
|
32 |
+
# # Set the environment variable (important for Hugging Face Spaces)
|
33 |
+
# api_key = os.environ.get("GEMINI_API_KEY")
|
34 |
+
# if not api_key:
|
35 |
+
# raise ValueError("GEMINI_API_KEY not found in environment variables.")
|
36 |
+
|
37 |
+
# os.environ["GOOGLE_API_KEY"] = api_key # Needed for google-genai Client
|
38 |
+
|
39 |
+
# # Initialize the Gemini client
|
40 |
+
# self.client = genai.Client()
|
41 |
+
|
42 |
+
# # Set model ID (you can choose another if needed)
|
43 |
+
# self.model_id = "gemini-2.0-flash-exp"
|
44 |
+
|
45 |
+
# # (Optional) Define generation config
|
46 |
+
# self.generation_config = types.GenerateContentConfig(
|
47 |
+
# temperature=0.4,
|
48 |
+
# top_p=0.95,
|
49 |
+
# top_k=20,
|
50 |
+
# candidate_count=1,
|
51 |
+
# seed=5,
|
52 |
+
# presence_penalty=0.0,
|
53 |
+
# frequency_penalty=0.0,
|
54 |
+
# )
|
55 |
+
|
56 |
+
# def __call__(self, question: str) -> str:
|
57 |
+
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
58 |
+
|
59 |
+
# try:
|
60 |
+
# response = self.client.models.generate_content(
|
61 |
+
# model=self.model_id,
|
62 |
+
# contents=f"Answer the following question clearly and concisely: {question}",
|
63 |
+
# config=self.generation_config
|
64 |
+
# )
|
65 |
+
# answer = response.text.strip()
|
66 |
+
# print(f"Agent returning answer (first 100 chars): {answer[:100]}")
|
67 |
+
# return answer
|
68 |
+
# except Exception as e:
|
69 |
+
# print(f"Error during Gemini API call: {str(e)}")
|
70 |
+
# return f"Error: {str(e)}"
|
71 |
+
|
72 |
+
class BasicAgent(ReActAgent):
|
73 |
def __init__(self):
|
74 |
+
print("BasicAgent using local LLM initialized.")
|
75 |
+
|
76 |
+
# Load a small model from Hugging Face
|
77 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # You can pick another lightweight model
|
78 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
79 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
80 |
+
model_name,
|
81 |
+
torch_dtype=torch.float16,
|
82 |
+
device_map="auto" # Automatically choose GPU/CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
|
85 |
+
super().__init__(tools=[]) # No tools for now
|
|
|
86 |
|
87 |
+
def call(self, task: str) -> str:
|
88 |
+
"""Core method for answering a task."""
|
89 |
+
prompt = f"Answer the following question concisely:\n\n{task}\n\nAnswer:"
|
90 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
|
91 |
+
|
92 |
+
with torch.no_grad():
|
93 |
+
outputs = self.model.generate(
|
94 |
+
**inputs,
|
95 |
+
max_new_tokens=200,
|
96 |
+
do_sample=True,
|
97 |
+
temperature=0.7,
|
98 |
+
top_p=0.95,
|
99 |
+
top_k=50,
|
100 |
)
|
101 |
+
answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
102 |
+
|
103 |
+
# Extract only the answer part
|
104 |
+
return answer.split("Answer:")[-1].strip()
|
|
|
|
|
105 |
|
106 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
107 |
"""
|