lukmanaj commited on
Commit
3f5e7c7
·
verified ·
1 Parent(s): a4b8b17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -76
app.py CHANGED
@@ -3,10 +3,8 @@ 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
- 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
- # class BasicAgent:
27
- # def __init__(self):
28
- # print("CustomAgent (using Gemini 2.0) initialized.")
29
-
30
- # # Set the environment variable (important for Hugging Face Spaces)
31
- # api_key = os.environ.get("GEMINI_API_KEY")
32
- # if not api_key:
33
- # raise ValueError("GEMINI_API_KEY not found in environment variables.")
34
-
35
- # os.environ["GOOGLE_API_KEY"] = api_key # Needed for google-genai Client
36
-
37
- # # Initialize the Gemini client
38
- # self.client = genai.Client()
39
-
40
- # # Set model ID (you can choose another if needed)
41
- # self.model_id = "gemini-2.0-flash-exp"
42
-
43
- # # (Optional) Define generation config
44
- # self.generation_config = types.GenerateContentConfig(
45
- # temperature=0.4,
46
- # top_p=0.95,
47
- # top_k=20,
48
- # candidate_count=1,
49
- # seed=5,
50
- # presence_penalty=0.0,
51
- # frequency_penalty=0.0,
52
- # )
53
 
54
- # def __call__(self, question: str) -> str:
55
- # print(f"Agent received question (first 50 chars): {question[:50]}...")
56
 
57
- # try:
58
- # response = self.client.models.generate_content(
59
- # model=self.model_id,
60
- # contents=f"Answer the following question clearly and concisely: {question}",
61
- # config=self.generation_config
62
- # )
63
- # answer = response.text.strip()
64
- # print(f"Agent returning answer (first 100 chars): {answer[:100]}")
65
- # return answer
66
- # except Exception as e:
67
- # print(f"Error during Gemini API call: {str(e)}")
68
- # return f"Error: {str(e)}"
 
 
 
 
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
- def __call__(self, task: str) -> str:
117
- """Answer a question."""
118
- prompt = f"Answer the following question clearly and concisely:\n\n{task}\n\nAnswer:"
119
- inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
120
-
121
- with torch.no_grad():
122
- outputs = self.model.generate(
123
- **inputs,
124
- max_new_tokens=256,
125
- do_sample=True,
126
- temperature=0.7,
127
- top_p=0.9,
128
- top_k=50,
129
- )
130
- decoded = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
- # Extract the answer part
133
- if "Answer:" in decoded:
134
- return decoded.split("Answer:")[-1].strip()
135
- return decoded.strip()
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
  """