Update app.py
Browse files
app.py
CHANGED
@@ -3,21 +3,66 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
-
# ---
|
12 |
-
# -----
|
13 |
-
class
|
14 |
def __init__(self):
|
15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def __call__(self, question: str) -> str:
|
17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
@@ -40,7 +85,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 google.genai import types
|
7 |
+
|
8 |
+
# Import the Google ADK agent components from agent.py
|
9 |
+
from agent import runner, session_service, APP_NAME, USER_ID, SESSION_ID
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
14 |
|
15 |
+
# --- Google ADK Agent Wrapper ---
|
16 |
+
# ----- USING THE ACTUAL GOOGLE ADK AGENT FROM AGENT.PY ------
|
17 |
+
class GoogleADKAgent:
|
18 |
def __init__(self):
|
19 |
+
print("GoogleADKAgent initialized with Google ADK runner and root_agent.")
|
20 |
+
self.runner = runner
|
21 |
+
self.session_service = session_service
|
22 |
+
self.app_name = APP_NAME
|
23 |
+
self.user_id = USER_ID
|
24 |
+
self.question_counter = 0 # To create unique session IDs for each question
|
25 |
+
|
26 |
def __call__(self, question: str) -> str:
|
27 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
28 |
+
try:
|
29 |
+
# Create a unique session for each question to avoid state conflicts
|
30 |
+
self.question_counter += 1
|
31 |
+
unique_session_id = f"{SESSION_ID}_q{self.question_counter}"
|
32 |
+
|
33 |
+
session = self.session_service.create_session(
|
34 |
+
app_name=self.app_name,
|
35 |
+
user_id=self.user_id,
|
36 |
+
session_id=unique_session_id
|
37 |
+
)
|
38 |
+
|
39 |
+
# Create the query content
|
40 |
+
query_content = types.Content(
|
41 |
+
role='user',
|
42 |
+
parts=[types.Part(text=question)]
|
43 |
+
)
|
44 |
+
|
45 |
+
# Run the agent synchronously using the runner
|
46 |
+
events = list(self.runner.run(session=session, new_message=query_content))
|
47 |
+
|
48 |
+
# Extract the final answer from the events
|
49 |
+
final_answer = "No response generated."
|
50 |
+
for event in reversed(events): # Look at events in reverse to find the last meaningful response
|
51 |
+
if event.content and event.content.parts:
|
52 |
+
for part in event.content.parts:
|
53 |
+
if part.text and part.text.strip():
|
54 |
+
final_answer = part.text.strip()
|
55 |
+
break
|
56 |
+
if final_answer != "No response generated.":
|
57 |
+
break
|
58 |
+
|
59 |
+
print(f"Agent returning answer: {final_answer[:100]}...")
|
60 |
+
return final_answer
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
error_msg = f"Error running Google ADK agent: {str(e)}"
|
64 |
+
print(error_msg)
|
65 |
+
return error_msg
|
66 |
|
67 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
68 |
"""
|
|
|
85 |
|
86 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
87 |
try:
|
88 |
+
agent = GoogleADKAgent()
|
89 |
except Exception as e:
|
90 |
print(f"Error instantiating agent: {e}")
|
91 |
return f"Error initializing agent: {e}", None
|