DarrenDsa commited on
Commit
6726bf5
·
verified ·
1 Parent(s): 81917a3

Agent Logic implemented

Browse files
Files changed (1) hide show
  1. app.py +40 -5
app.py CHANGED
@@ -3,6 +3,8 @@ 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 +12,47 @@ 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 CodeAgent, HfApiModel, PromptTemplates, OpenAIServerModel
7
+ from tools import search_tool, speech_to_text, python_interpreter
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
 
12
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
+ if os.getenv("ENV") != "production":
16
+ load_dotenv()
17
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
18
+
19
+ # --- Basic Agent Definition ---
20
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
21
+ # model = HfApiModel()
22
+ model = OpenAIServerModel(model_id="gpt-4.1", api_key=OPENAI_API_KEY)
23
+ system_prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
24
+
25
  class BasicAgent:
26
  def __init__(self):
27
+ self.agent = CodeAgent(
28
+ tools=[search_tool, speech_to_text, python_interpreter],
29
+ model=model,
30
+ # max_steps=5,
31
+ # add_base_tools=True,
32
+ # prompt_templates=PromptTemplates(system_prompt=system_prompt),
33
+ additional_authorized_imports=[
34
+ "os",
35
+ "io",
36
+ "pathlib",
37
+ "json",
38
+ "csv",
39
+ "pandas",
40
+ "openpyxl",
41
+ "zipfile",
42
+ "mutagen",
43
+ "PIL"
44
+ ]
45
+ )
46
+ print("Agent initialized.")
47
+ def __call__(self, question: str, file_name: str) -> str:
48
  print(f"Agent received question (first 50 chars): {question[:50]}...")
49
+ task = f"{system_prompt} \n{question}"
50
+ if file_name != "":
51
+ response = self.agent.run(task=task, additional_args=file_name)
52
+ elif file_name == "":
53
+ response = self.agent.run(task)
54
+ print(f"Agent's answer: {response}")
55
+ return response
56
 
57
  def run_and_submit_all( profile: gr.OAuthProfile | None):
58
  """