Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
import torch
|
| 9 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
|
@@ -23,49 +21,49 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 23 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 24 |
# return fixed_answer
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# seed=5,
|
| 50 |
-
# presence_penalty=0.0,
|
| 51 |
-
# frequency_penalty=0.0,
|
| 52 |
-
# )
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# class BasicAgent(ReActAgent):
|
| 71 |
# def __init__(self):
|
|
@@ -100,39 +98,40 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 100 |
|
| 101 |
# # Extract only the answer part
|
| 102 |
# return answer.split("Answer:")[-1].strip()
|
| 103 |
-
class BasicAgent:
|
| 104 |
-
def __init__(self):
|
| 105 |
-
print("BasicAgent using local LLM initialized.")
|
| 106 |
-
|
| 107 |
-
# Load a small Hugging Face model
|
| 108 |
-
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # Change if you want
|
| 109 |
-
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 110 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
| 111 |
-
model_name,
|
| 112 |
-
torch_dtype=torch.float16,
|
| 113 |
-
device_map="auto" # Use GPU if available
|
| 114 |
-
)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
|
| 137 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 138 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from google import genai
|
| 7 |
+
from google.genai import types
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 21 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 22 |
# return fixed_answer
|
| 23 |
|
| 24 |
+
class BasicAgent:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
print("CustomAgent using Gemini 2.0 initialized.")
|
| 27 |
+
|
| 28 |
+
# Set up Gemini API
|
| 29 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
| 30 |
+
if not api_key:
|
| 31 |
+
raise ValueError("GEMINI_API_KEY not found in environment variables.")
|
| 32 |
+
|
| 33 |
+
os.environ["GOOGLE_API_KEY"] = api_key # Required by google-genai
|
| 34 |
+
|
| 35 |
+
self.client = genai.Client()
|
| 36 |
+
self.model_id = "gemini-2.0-flash-exp" # Or "gemini-1.5-flash-002" if you want faster
|
| 37 |
+
|
| 38 |
+
self.generation_config = types.GenerateContentConfig(
|
| 39 |
+
temperature=0.4,
|
| 40 |
+
top_p=0.9,
|
| 41 |
+
top_k=40,
|
| 42 |
+
candidate_count=1,
|
| 43 |
+
seed=42,
|
| 44 |
+
presence_penalty=0.0,
|
| 45 |
+
frequency_penalty=0.0,
|
| 46 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
def __call__(self, question: str) -> str:
|
| 49 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 50 |
|
| 51 |
+
try:
|
| 52 |
+
response = self.client.models.generate_content(
|
| 53 |
+
model=self.model_id,
|
| 54 |
+
contents=f"""You are a smart, factual assistant. Answer clearly and concisely:\n\n{question}\n\nProvide only the final answer without extra commentary.""",
|
| 55 |
+
config=self.generation_config,
|
| 56 |
+
)
|
| 57 |
+
answer = response.text.strip()
|
| 58 |
+
|
| 59 |
+
# ✨ Add a short sleep to avoid hitting rate limits
|
| 60 |
+
time.sleep(7) # Wait 7 seconds after each question
|
| 61 |
+
print(f"Returning answer (first 100 chars): {answer[:100]}")
|
| 62 |
+
return answer
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"Error during Gemini call: {str(e)}")
|
| 66 |
+
return f"Error: {str(e)}"
|
| 67 |
|
| 68 |
# class BasicAgent(ReActAgent):
|
| 69 |
# def __init__(self):
|
|
|
|
| 98 |
|
| 99 |
# # Extract only the answer part
|
| 100 |
# return answer.split("Answer:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# class BasicAgent:
|
| 103 |
+
# def __init__(self):
|
| 104 |
+
# print("BasicAgent using local LLM initialized.")
|
| 105 |
+
|
| 106 |
+
# # Load a small Hugging Face model
|
| 107 |
+
# model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # Change if you want
|
| 108 |
+
# self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 109 |
+
# self.model = AutoModelForCausalLM.from_pretrained(
|
| 110 |
+
# model_name,
|
| 111 |
+
# torch_dtype=torch.float16,
|
| 112 |
+
# device_map="auto" # Use GPU if available
|
| 113 |
+
# )
|
| 114 |
+
|
| 115 |
+
# def __call__(self, task: str) -> str:
|
| 116 |
+
# """Answer a question."""
|
| 117 |
+
# prompt = f"Answer the following question clearly and concisely:\n\n{task}\n\nAnswer:"
|
| 118 |
+
# inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
|
| 119 |
+
|
| 120 |
+
# with torch.no_grad():
|
| 121 |
+
# outputs = self.model.generate(
|
| 122 |
+
# **inputs,
|
| 123 |
+
# max_new_tokens=256,
|
| 124 |
+
# do_sample=True,
|
| 125 |
+
# temperature=0.7,
|
| 126 |
+
# top_p=0.9,
|
| 127 |
+
# top_k=50,
|
| 128 |
+
# )
|
| 129 |
+
# decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 130 |
|
| 131 |
+
# # Extract the answer part
|
| 132 |
+
# if "Answer:" in decoded:
|
| 133 |
+
# return decoded.split("Answer:")[-1].strip()
|
| 134 |
+
# return decoded.strip()
|
| 135 |
|
| 136 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 137 |
"""
|