agaliano commited on
Commit
59838e5
·
verified ·
1 Parent(s): fb27e07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -3
app.py CHANGED
@@ -3,6 +3,10 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -10,14 +14,73 @@ 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
  class BasicAgent:
14
  def __init__(self):
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  print("BasicAgent initialized.")
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import LiteLLMModel
7
+ from smolagents import CodeAgent, ToolCallingAgent, GoogleSearchTool, HfApiModel, VisitWebpageTool, DuckDuckGoSearchTool, tool, PythonInterpreterTool, WikipediaSearchTool
8
+ import requests
9
+ from youtube_transcript_api import YouTubeTranscriptApi
10
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
 
14
 
15
  # --- Basic Agent Definition ---
16
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
17
+ @tool
18
+ def reverse_sentence_tool(reverse_sentence: str) -> str:
19
+ """
20
+ This tool receives a sentence where both the word order and the characters in each word are reversed.
21
+ It returns the sentence with words and order corrected.
22
+ Args:
23
+ reverse_sentence: A sentence with reversed words and reversed word order.
24
+ Returns:
25
+ A sentence in natural reading order.
26
+ """
27
+ inverted_words = reverse_sentence.split(" ")[::-1]
28
+ correct_words = [word[::-1] for word in inverted_words]
29
+
30
+ return " ".join(correct_words)
31
+
32
+ @tool
33
+ def get_youtube_transcript(video_url: str) -> str:
34
+ """
35
+ Fetches the transcript from a YouTube video if available.
36
+ Args:
37
+ video_url: Full URL to the YouTube video.
38
+ Returns:
39
+ Transcript text.
40
+ """
41
+ video_id = video_url.split("v=")[-1]
42
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
43
+ full_text = " ".join([entry['text'] for entry in transcript])
44
+ return full_text
45
+
46
+ @tool
47
+ def check_answer(answer: str) -> str:
48
+ """
49
+ Reviews the answer to check that it meets the requirements specified by the user and modifies it if necessary.
50
+ Args:
51
+ answer (str): The answer fo the Agent.
52
+ Returns:
53
+ str: The final answer.
54
+ """
55
+
56
+ if answer[-1] == '.':
57
+ return answer[:-1]
58
+
59
+ if "St." in answer:
60
+ return answer.replace("St.", "Saint")
61
+
62
+ return answer
63
+
64
  class BasicAgent:
65
  def __init__(self):
66
+ self.api_key = os.getenv("GOOGLE_API_KEY")
67
+ self.model = LiteLLMModel(model_id="gemini/gemini-2.0-flash", api_key=self.api_key)
68
+
69
+ self.agent = CodeAgent(
70
+ tools=[
71
+ DuckDuckGoSearchTool(),
72
+ PythonInterpreterTool(),
73
+ VisitWebpageTool(),
74
+ reverse_sentence_tool,
75
+ check_answer
76
+ ],
77
+ model=self.model
78
+ )
79
  print("BasicAgent initialized.")
80
  def __call__(self, question: str) -> str:
81
  print(f"Agent received question (first 50 chars): {question[:50]}...")
82
+ answer = self.agent.run(question)
83
+ return answer
 
84
 
85
  def run_and_submit_all( profile: gr.OAuthProfile | None):
86
  """