dawid-lorek commited on
Commit
3bce169
·
verified ·
1 Parent(s): 47cf71f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -81
app.py CHANGED
@@ -1,83 +1,21 @@
1
- # app.py (Gradio version with LangChain agent)
2
 
3
  import os
4
- import requests
5
  import pandas as pd
 
6
  import gradio as gr
7
- from typing import List
8
-
9
- from langchain.agents import initialize_agent, AgentType, Tool
10
- from langchain_community.tools import DuckDuckGoSearchRun
11
- from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
12
- from langchain_experimental.tools.python.tool import PythonREPLTool
13
- from langchain_community.tools.youtube.search import YouTubeSearchTool
14
- from langchain_community.document_loaders import YoutubeLoader
15
- from langchain_openai import ChatOpenAI
16
- from langchain.tools import tool
17
-
18
- # --- LangChain LLM and Tools Setup --- #
19
- llm = ChatOpenAI(model="gpt-4o", temperature=0)
20
-
21
- @tool
22
- def get_yt_transcript(url: str) -> str:
23
- loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
24
- docs = loader.load()
25
- return " ".join(doc.page_content for doc in docs)
26
-
27
- @tool
28
- def reverse_sentence_logic(sentence: str) -> str:
29
- return sentence[::-1]
30
-
31
- @tool
32
- def commutativity_counterexample(_: str) -> str:
33
- return "a, b, c"
34
-
35
- @tool
36
- def malko_winner(_: str) -> str:
37
- return "Uroš"
38
 
39
- @tool
40
- def ray_actor_answer(_: str) -> str:
41
- return "Filip"
42
-
43
- @tool
44
- def chess_position_hint(_: str) -> str:
45
- return "Qd1+"
46
-
47
- @tool
48
- def default_award_number(_: str) -> str:
49
- return "80NSSC21K1030"
50
-
51
- # Add your LangChain tools here
52
- langchain_tools: List[Tool] = [
53
- DuckDuckGoSearchRun(),
54
- WikipediaQueryRun(api_wrapper=None),
55
- YouTubeSearchTool(),
56
- Tool(name="youtube_transcript", func=get_yt_transcript, description="Transcribe YouTube video from URL"),
57
- PythonREPLTool(),
58
- reverse_sentence_logic,
59
- commutativity_counterexample,
60
- malko_winner,
61
- ray_actor_answer,
62
- chess_position_hint,
63
- default_award_number,
64
- ]
65
-
66
- agent = initialize_agent(tools=langchain_tools, llm=llm, agent=AgentType.OPENAI_MULTI_FUNCTIONS, verbose=False)
67
-
68
- # --- Hugging Face Evaluation Integration --- #
69
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
70
 
71
- class LangChainAgent:
72
  def __init__(self):
73
- print("LangChainAgent initialized.")
74
 
75
  def __call__(self, question: str) -> str:
76
- print(f"Running agent on: {question[:60]}")
77
- try:
78
- return agent.run(question)
79
- except Exception as e:
80
- return f"[ERROR] {str(e)}"
81
 
82
  def run_and_submit_all(profile: gr.OAuthProfile | None):
83
  space_id = os.getenv("SPACE_ID")
@@ -88,7 +26,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
88
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
89
  api_url = DEFAULT_API_URL
90
 
91
- # Fetch questions
92
  try:
93
  response = requests.get(f"{api_url}/questions", timeout=15)
94
  response.raise_for_status()
@@ -98,44 +35,45 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
98
 
99
  answers_payload = []
100
  results_log = []
101
- bot = LangChainAgent()
102
 
103
  for item in questions_data:
104
  q = item.get("question")
105
  task_id = item.get("task_id")
106
  try:
107
- a = bot(q)
108
  except Exception as e:
109
  a = f"ERROR: {e}"
110
  answers_payload.append({"task_id": task_id, "submitted_answer": a})
111
  results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": a})
112
 
113
- submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
114
 
115
- # Submit answers
116
  try:
117
  response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60)
118
  response.raise_for_status()
119
  result_data = response.json()
120
- final_status = (
121
- f"Submission Successful!\n"
122
  f"User: {result_data.get('username')}\n"
123
  f"Score: {result_data.get('score')}%\n"
124
  f"Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}\n"
125
  f"Message: {result_data.get('message')}"
126
  )
127
- return final_status, pd.DataFrame(results_log)
128
  except Exception as e:
129
  return f"Submission failed: {e}", pd.DataFrame(results_log)
130
 
131
- # --- Gradio UI --- #
132
  with gr.Blocks() as demo:
133
- gr.Markdown("# LangChain GAIA Agent – Evaluation Portal")
134
  gr.LoginButton()
135
  run_btn = gr.Button("Run Evaluation & Submit All Answers")
136
  status_box = gr.Textbox(label="Status", lines=5)
137
  result_table = gr.DataFrame(label="Agent Answers")
138
-
139
  run_btn.click(fn=run_and_submit_all, outputs=[status_box, result_table])
140
 
141
  demo.launch(debug=True)
 
1
+ # app.py
2
 
3
  import os
4
+ import asyncio
5
  import pandas as pd
6
+ import requests
7
  import gradio as gr
8
+ from agent import answer_question
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
+ class GAIALlamaAgent:
13
  def __init__(self):
14
+ print("Initialized LlamaIndex Agent")
15
 
16
  def __call__(self, question: str) -> str:
17
+ print(f"Received question: {question[:60]}...")
18
+ return asyncio.run(answer_question(question))
 
 
 
19
 
20
  def run_and_submit_all(profile: gr.OAuthProfile | None):
21
  space_id = os.getenv("SPACE_ID")
 
26
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
27
  api_url = DEFAULT_API_URL
28
 
 
29
  try:
30
  response = requests.get(f"{api_url}/questions", timeout=15)
31
  response.raise_for_status()
 
35
 
36
  answers_payload = []
37
  results_log = []
38
+ agent = GAIALlamaAgent()
39
 
40
  for item in questions_data:
41
  q = item.get("question")
42
  task_id = item.get("task_id")
43
  try:
44
+ a = agent(q)
45
  except Exception as e:
46
  a = f"ERROR: {e}"
47
  answers_payload.append({"task_id": task_id, "submitted_answer": a})
48
  results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": a})
49
 
50
+ submission_data = {
51
+ "username": username,
52
+ "agent_code": agent_code,
53
+ "answers": answers_payload
54
+ }
55
 
 
56
  try:
57
  response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60)
58
  response.raise_for_status()
59
  result_data = response.json()
60
+ status = (
61
+ f"Submission Successful!\n"
62
  f"User: {result_data.get('username')}\n"
63
  f"Score: {result_data.get('score')}%\n"
64
  f"Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}\n"
65
  f"Message: {result_data.get('message')}"
66
  )
67
+ return status, pd.DataFrame(results_log)
68
  except Exception as e:
69
  return f"Submission failed: {e}", pd.DataFrame(results_log)
70
 
 
71
  with gr.Blocks() as demo:
72
+ gr.Markdown("# LlamaIndex GAIA Agent – Evaluation Portal")
73
  gr.LoginButton()
74
  run_btn = gr.Button("Run Evaluation & Submit All Answers")
75
  status_box = gr.Textbox(label="Status", lines=5)
76
  result_table = gr.DataFrame(label="Agent Answers")
 
77
  run_btn.click(fn=run_and_submit_all, outputs=[status_box, result_table])
78
 
79
  demo.launch(debug=True)