rakesh-dvg commited on
Commit
ebcda24
·
verified ·
1 Parent(s): 0c1ed4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -151
app.py CHANGED
@@ -1,157 +1,128 @@
1
- """ Basic Agent Evaluation Runner"""
2
  import os
3
- import gradio as gr
4
- import requests
5
- import pandas as pd
6
- from langchain_core.messages import HumanMessage
7
- from agent import build_graph
8
-
9
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
-
11
- class BasicAgent:
12
- """A langgraph agent."""
13
- def __init__(self):
14
- print("BasicAgent initialized.")
15
- self.graph = build_graph()
16
-
17
- def __call__(self, question: str) -> str:
18
- print(f"Agent received question (first 50 chars): {question[:50]}...")
19
- messages = [HumanMessage(content=question)]
20
- messages = self.graph.invoke({"messages": messages})
21
- answer = messages['messages'][-1].content
22
- print(f"Raw answer: {answer}")
23
- return answer.strip()
24
-
25
-
26
- def run_and_submit_all(profile: gr.OAuthProfile | None):
27
- space_id = os.getenv("SPACE_ID")
28
-
29
- if profile:
30
- username = profile.username
31
- print(f"User logged in: {username}")
32
- else:
33
- print("User not logged in.")
34
- return "Please Login to Hugging Face with the button.", None
35
-
36
- api_url = DEFAULT_API_URL
37
- questions_url = f"{api_url}/questions"
38
- submit_url = f"{api_url}/submit"
39
-
40
- try:
41
- agent = BasicAgent()
42
- except Exception as e:
43
- print(f"Error instantiating agent: {e}")
44
- return f"Error initializing agent: {e}", None
45
-
46
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available."
47
-
48
- try:
49
- response = requests.get(questions_url, timeout=15)
50
- response.raise_for_status()
51
- questions_data = response.json()
52
- if not questions_data:
53
- return "Fetched questions list is empty or invalid format.", None
54
- except Exception as e:
55
- print(f"Error fetching questions: {e}")
56
- return f"Error fetching questions: {e}", None
57
-
58
- results_log = []
59
- answers_payload = []
60
- for item in questions_data:
61
- task_id = item.get("task_id")
62
- question_text = item.get("question")
63
- if not task_id or question_text is None:
64
- continue
65
- try:
66
- submitted_answer = agent(question_text)
67
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
68
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
69
- except Exception as e:
70
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
71
-
72
- if not answers_payload:
73
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
74
-
75
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
76
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
77
-
78
- try:
79
- response = requests.post(submit_url, json=submission_data, timeout=60)
80
- response.raise_for_status()
81
- result_data = response.json()
82
- final_status = (
83
- f"Submission Successful!\n"
84
- f"User: {result_data.get('username')}\n"
85
- f"Overall Score: {result_data.get('score', 'N/A')}% "
86
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
87
- f"Message: {result_data.get('message', 'No message received.')}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  )
89
- results_df = pd.DataFrame(results_log)
90
- return final_status, results_df
91
- except requests.exceptions.HTTPError as e:
92
- error_detail = f"Server responded with status {e.response.status_code}."
93
- try:
94
- error_json = e.response.json()
95
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
96
- except Exception:
97
- error_detail += f" Response: {e.response.text[:500]}"
98
- status_message = f"Submission Failed: {error_detail}"
99
- results_df = pd.DataFrame(results_log)
100
- return status_message, results_df
101
- except requests.exceptions.Timeout:
102
- status_message = "Submission Failed: The request timed out."
103
- results_df = pd.DataFrame(results_log)
104
- return status_message, results_df
105
- except requests.exceptions.RequestException as e:
106
- status_message = f"Submission Failed: Network error - {e}"
107
- results_df = pd.DataFrame(results_log)
108
- return status_message, results_df
109
- except Exception as e:
110
- status_message = f"An unexpected error occurred during submission: {e}"
111
- results_df = pd.DataFrame(results_log)
112
- return status_message, results_df
113
-
114
-
115
- with gr.Blocks() as demo:
116
- gr.Markdown("# Basic Agent Evaluation Runner")
117
- gr.Markdown(
118
- """
119
- **Instructions:**
120
-
121
- 1. Clone this space and modify the code to define your agent.
122
- 2. Log in with Hugging Face account using the button below.
123
- 3. Click 'Run Evaluation & Submit All Answers' to evaluate your agent and submit answers.
124
-
125
- ---
126
- **Note:**
127
- This is a simple baseline. Consider improving with caching or async processing.
128
- """
129
- )
130
-
131
- gr.LoginButton()
132
- run_button = gr.Button("Run Evaluation & Submit All Answers")
133
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
134
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
135
-
136
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
137
-
138
- if __name__ == "__main__":
139
- print("\n" + "-"*30 + " App Starting " + "-"*30)
140
- space_host_startup = os.getenv("SPACE_HOST")
141
- space_id_startup = os.getenv("SPACE_ID")
142
 
143
- if space_host_startup:
144
- print(f"✅ SPACE_HOST found: {space_host_startup}")
145
- print(f" Runtime URL: https://{space_host_startup}.hf.space")
146
  else:
147
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
148
 
149
- if space_id_startup:
150
- print(f"✅ SPACE_ID found: {space_id_startup}")
151
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
152
- else:
153
- print("ℹ️ SPACE_ID environment variable not found (running locally?).")
154
 
155
- print("-" * (60 + len(" App Starting ")) + "\n")
156
- print("Launching Gradio Interface for Basic Agent Evaluation...")
157
- demo.launch(debug=True, share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """LangGraph Agent (GROQ version without Supabase)"""
2
  import os
3
+ from langgraph.graph import START, StateGraph, MessagesState
4
+ from langgraph.prebuilt import tools_condition, ToolNode
5
+ from langchain_google_genai import ChatGoogleGenerativeAI
6
+ from langchain_groq import ChatGroq
7
+ from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
8
+ from langchain_community.tools.tavily_search import TavilySearchResults
9
+ from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
10
+ from langchain_core.messages import SystemMessage, HumanMessage
11
+ from langchain_core.tools import tool
12
+
13
+ @tool
14
+ def multiply(a: int, b: int) -> int:
15
+ """Multiply two numbers."""
16
+ return a * b
17
+
18
+ @tool
19
+ def add(a: int, b: int) -> int:
20
+ """Add two numbers."""
21
+ return a + b
22
+
23
+ @tool
24
+ def subtract(a: int, b: int) -> int:
25
+ """Subtract second number from the first."""
26
+ return a - b
27
+
28
+ @tool
29
+ def divide(a: int, b: int) -> float:
30
+ """Divide two numbers."""
31
+ if b == 0:
32
+ raise ValueError("Cannot divide by zero.")
33
+ return a / b
34
+
35
+ @tool
36
+ def modulus(a: int, b: int) -> int:
37
+ """Get the modulus (remainder) of two numbers."""
38
+ return a % b
39
+
40
+ @tool
41
+ def wiki_search(query: str) -> dict:
42
+ """Search Wikipedia for a query and return up to 2 results."""
43
+ search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
44
+ formatted_search_docs = "\n\n---\n\n".join(
45
+ [
46
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
47
+ for doc in search_docs
48
+ ])
49
+ return {"wiki_results": formatted_search_docs}
50
+
51
+ @tool
52
+ def web_search(query: str) -> dict:
53
+ """Search Tavily for a query and return up to 3 results."""
54
+ search_docs = TavilySearchResults(max_results=3).invoke(query=query)
55
+ formatted_search_docs = "\n\n---\n\n".join(
56
+ [
57
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
58
+ for doc in search_docs
59
+ ])
60
+ return {"web_results": formatted_search_docs}
61
+
62
+ @tool
63
+ def arvix_search(query: str) -> dict:
64
+ """Search Arxiv and return up to 3 truncated results."""
65
+ search_docs = ArxivLoader(query=query, load_max_docs=3).load()
66
+ formatted_search_docs = "\n\n---\n\n".join(
67
+ [
68
+ f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
69
+ for doc in search_docs
70
+ ])
71
+ return {"arvix_results": formatted_search_docs}
72
+
73
+ # Load system prompt
74
+ with open("system_prompt.txt", "r", encoding="utf-8") as f:
75
+ system_prompt = f.read()
76
+
77
+ sys_msg = SystemMessage(content=system_prompt)
78
+
79
+ tools = [
80
+ multiply, add, subtract, divide, modulus,
81
+ wiki_search, web_search, arvix_search
82
+ ]
83
+
84
+ def build_graph(provider: str = "groq"):
85
+ """Build the LangGraph agent using specified LLM provider."""
86
+
87
+ if provider == "google":
88
+ llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
89
+
90
+ elif provider == "groq":
91
+ groq_api_key = os.getenv("GROQ_API_KEY")
92
+ if not groq_api_key:
93
+ raise ValueError("GROQ_API_KEY environment variable not set.")
94
+ llm = ChatGroq(model="qwen-qwq-32b", temperature=0, api_key=groq_api_key)
95
+
96
+ elif provider == "huggingface":
97
+ llm = ChatHuggingFace(
98
+ llm=HuggingFaceEndpoint(
99
+ url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
100
+ temperature=0,
101
+ )
102
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
 
 
 
104
  else:
105
+ raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")
106
 
107
+ llm_with_tools = llm.bind_tools(tools)
 
 
 
 
108
 
109
+ def assistant(state: MessagesState):
110
+ return {"messages": [llm_with_tools.invoke(state["messages"])]}
111
+
112
+ builder = StateGraph(MessagesState)
113
+ builder.add_node("assistant", assistant)
114
+ builder.add_node("tools", ToolNode(tools))
115
+ builder.add_edge(START, "assistant")
116
+ builder.add_conditional_edges("assistant", tools_condition)
117
+ builder.add_edge("tools", "assistant")
118
+
119
+ return builder.compile()
120
+
121
+ # For testing purposes
122
+ if __name__ == "__main__":
123
+ question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
124
+ graph = build_graph(provider="groq")
125
+ messages = [HumanMessage(content=question)]
126
+ result = graph.invoke({"messages": messages})
127
+ for msg in result["messages"]:
128
+ print(msg.content)