Youssef-El-SaYed commited on
Commit
11a4b93
·
verified ·
1 Parent(s): 2271161

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -21
app.py CHANGED
@@ -1,35 +1,78 @@
1
  import os
2
  import gradio as gr
3
- import requests
4
  import inspect
5
  import pandas as pd
6
- from langchain_core.messages import HumanMessage
7
- from huggingface_hub import InferenceClient
8
- from agent import build_graph
9
- from dotenv import load_dotenv
10
- from supabase import create_client
11
- load_dotenv() # This reads the .env file
12
 
13
- # (Keep Constants as is)
14
- # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
  # --- Basic Agent Definition ---
18
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
19
- class BasicAgent:
20
- """A langgraph agent."""
21
- def __init__(self):
22
- print("BasicAgent initialized.")
23
- self.graph = build_graph()
 
 
 
 
 
 
24
 
25
- def __call__(self, question: str) -> str:
26
- print(f"Agent received question (first 50 chars): {question[:50]}...")
27
- messages = [HumanMessage(content=question)]
28
- result = self.graph.invoke({"messages": messages})
29
- answer = result['messages'][-1].content
30
- return answer # kein [14:] mehr nötig!
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def run_and_submit_all( profile: gr.OAuthProfile | None):
35
  """
@@ -52,7 +95,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
52
 
53
  # 1. Instantiate Agent ( modify this part to create your agent)
54
  try:
55
- agent = BasicAgent()
56
  except Exception as e:
57
  print(f"Error instantiating agent: {e}")
58
  return f"Error initializing agent: {e}", None
 
1
  import os
2
  import gradio as gr
 
3
  import inspect
4
  import pandas as pd
5
+ from PIL import Image
6
+ import requests
7
+ from io import BytesIO
 
 
 
8
 
 
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
+ from smolagents import load_tool, SpeechToTextTool, FinalAnswerTool, OpenAIServerModel, CodeAgent, \
14
+ VisitWebpageTool, tool, GoogleSearchTool
15
+ import pandas
16
+ import numpy
17
+ from dotenv import load_dotenv
18
+ import os
19
+
20
+ # SET CONSTANTS
21
+ load_dotenv()
22
+ MODEL_ID = "gpt-4o"
23
+ OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
24
 
25
+ # SETUP MODEL
26
+ model = OpenAIServerModel(
27
+ model_id=MODEL_ID,
28
+ api_key=OPEN_AI_KEY
29
+ )
 
30
 
31
 
32
+ # WIKIPEDIA TOOLS
33
+ @tool
34
+ def search_wikipedia(search_term: str, results: int = 5) -> str:
35
+ """
36
+ Retrieve the article title(s) for a given topic in Wikipedia. This is useful for determining what to use as input
37
+ in the view_wikipedia tool to see the full article.
38
+ Args:
39
+ search_term: the topic to search for
40
+ results: the number of search results to return
41
+ """
42
+ import wikipedia
43
+ search_results = wikipedia.search(search_term, results=results)
44
+ return search_results
45
+
46
+
47
+ @tool
48
+ def view_wikipedia(article_name: str) -> str:
49
+ """
50
+ Retrieve the full text for a given Wikipedia article.
51
+ Args:
52
+ article_name: the title of the article you want to view. If you are unsure, this can be found using the search_wikipedia tool.
53
+ """
54
+ import wikipedia
55
+ wikipedia_page = wikipedia.page(article_name)
56
+ article_title = wikipedia_page.title
57
+ article_text = wikipedia_page.content
58
+ return f"### Title: {article_title} ### Text: {article_text}"
59
+
60
+
61
+ # SETUP TOOLS
62
+ youtube_transcript_extractor = load_tool("Mightypeacock/tool-YoutubeTranscript", trust_remote_code=True)
63
+ visit_webpage = VisitWebpageTool()
64
+ search_tool = GoogleSearchTool()
65
+ speech_to_text = SpeechToTextTool()
66
+ final_answer = FinalAnswerTool()
67
+
68
+ # SETUP AND RUN AGENT
69
+ my_agent = CodeAgent(
70
+ model=model,
71
+ tools=[youtube_transcript_extractor, visit_webpage, search_tool, final_answer, speech_to_text],
72
+ add_base_tools=True,
73
+ additional_authorized_imports=["pandas", "numpy"],
74
+ max_steps=7
75
+ )
76
 
77
  def run_and_submit_all( profile: gr.OAuthProfile | None):
78
  """
 
95
 
96
  # 1. Instantiate Agent ( modify this part to create your agent)
97
  try:
98
+ agent = my_agent
99
  except Exception as e:
100
  print(f"Error instantiating agent: {e}")
101
  return f"Error initializing agent: {e}", None