YajieXu commited on
Commit
5a04eaf
·
verified ·
1 Parent(s): 0b2a728

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -6
app.py CHANGED
@@ -1,17 +1,21 @@
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
- from transformers import pipeline
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
 
10
  # --- Enhanced Agent Definition ---
11
  class GAIAAgent:
12
  def __init__(self):
13
- print("GAIAAgent initialized.")
14
- self.model = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")
 
 
15
 
16
  def format_prompt(self, question: str, file_content: str = None) -> str:
17
  prompt = (
@@ -33,8 +37,18 @@ class GAIAAgent:
33
  def __call__(self, question: str, file_name: str = None) -> str:
34
  file_content = self.read_file(file_name) if file_name else None
35
  prompt = self.format_prompt(question, file_content)
36
- result = self.model(prompt, max_new_tokens=100)[0]["generated_text"]
37
- return result.strip().split("Answer:")[-1].strip()
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  def run_and_submit_all(profile: gr.OAuthProfile | None):
@@ -120,4 +134,4 @@ with gr.Blocks() as demo:
120
 
121
  if __name__ == "__main__":
122
  print("Launching GAIA agent app...")
123
- demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
4
+ import inspect
5
  import pandas as pd
6
+
7
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+ # --- Enhanced Agent Definition ---
12
  # --- Enhanced Agent Definition ---
13
  class GAIAAgent:
14
  def __init__(self):
15
+ self.api_token = os.getenv("HF_TOKEN")
16
+ self.model_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
17
+ self.api_url = f"https://api-inference.huggingface.co/models/{self.model_id}"
18
+ print("GAIAAgent with Hugging Face Inference API initialized.")
19
 
20
  def format_prompt(self, question: str, file_content: str = None) -> str:
21
  prompt = (
 
37
  def __call__(self, question: str, file_name: str = None) -> str:
38
  file_content = self.read_file(file_name) if file_name else None
39
  prompt = self.format_prompt(question, file_content)
40
+ headers = {
41
+ "Authorization": f"Bearer {self.api_token}",
42
+ "Content-Type": "application/json",
43
+ }
44
+ response = requests.post(self.api_url, headers=headers, json={"inputs": prompt})
45
+ result = response.json()
46
+
47
+ if isinstance(result, list) and "generated_text" in result[0]:
48
+ return result[0]["generated_text"].split("Answer:")[-1].strip()
49
+ else:
50
+ print(f"Unexpected response: {result}")
51
+ return "AGENT ERROR: Model inference failed."
52
 
53
 
54
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
134
 
135
  if __name__ == "__main__":
136
  print("Launching GAIA agent app...")
137
+ demo.launch(debug=True, share=False)