ATK20 commited on
Commit
168cfe8
·
verified ·
1 Parent(s): a0f06b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -26
app.py CHANGED
@@ -1,46 +1,32 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import openai
5
  import pandas as pd
 
6
 
7
  # Constants
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
- OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Assuming you're using OpenAI's GPT model for the agent.
10
 
11
- # Basic Agent Definition
12
  class BasicAgent:
13
  def __init__(self):
14
  print("BasicAgent initialized.")
15
- openai.api_key = OPENAI_API_KEY # Set OpenAI API key for GPT
16
 
17
  def __call__(self, question: str) -> str:
18
  print(f"Agent received question: {question[:50]}...")
19
-
20
- # Use OpenAI GPT to generate a response for the question
21
  try:
22
- response = openai.Completion.create(
23
- engine="text-davinci-003", # or another GPT engine
24
- prompt=question,
25
- max_tokens=150,
26
- n=1,
27
- stop=None,
28
- temperature=0.7,
29
- )
30
- fixed_answer = response.choices[0].text.strip()
31
- print(f"Agent returning answer: {fixed_answer}")
32
- return fixed_answer
33
  except Exception as e:
34
- print(f"Error while fetching response from GPT: {e}")
35
  return f"Error: {e}"
36
 
37
  def run_and_submit_all(profile: gr.OAuthProfile | None):
38
- """
39
- Fetches all questions, runs the BasicAgent on them, submits all answers,
40
- and displays the results.
41
- """
42
- # --- Determine HF Space Runtime URL and Repo URL ---
43
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
44
 
45
  if profile:
46
  username = f"{profile.username}"
@@ -103,7 +89,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
103
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
104
 
105
  # 4. Prepare Submission
106
- submission_data = {"username": username.strip(), "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main", "answers": answers_payload}
 
 
 
 
107
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
108
  print(status_update)
109
 
@@ -134,7 +124,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
134
  results_df = pd.DataFrame(results_log)
135
  return status_message, results_df
136
 
137
-
138
  # Gradio Interface
139
  with gr.Blocks() as demo:
140
  gr.Markdown("# Basic Agent Evaluation Runner")
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ from huggingface_hub import InferenceClient
6
 
7
  # Constants
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
9
 
10
+ # Basic Agent Definition using HF Inference API
11
  class BasicAgent:
12
  def __init__(self):
13
  print("BasicAgent initialized.")
14
+ self.client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
15
 
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question: {question[:50]}...")
 
 
18
  try:
19
+ prompt = f"[INST] {question.strip()} [/INST]"
20
+ response = self.client.text_generation(prompt=prompt, max_new_tokens=300, temperature=0.7)
21
+ answer = response.strip()
22
+ print(f"Agent returning answer: {answer}")
23
+ return answer
 
 
 
 
 
 
24
  except Exception as e:
25
+ print(f"Error while querying HF model: {e}")
26
  return f"Error: {e}"
27
 
28
  def run_and_submit_all(profile: gr.OAuthProfile | None):
29
+ space_id = os.getenv("SPACE_ID")
 
 
 
 
 
30
 
31
  if profile:
32
  username = f"{profile.username}"
 
89
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
90
 
91
  # 4. Prepare Submission
92
+ submission_data = {
93
+ "username": username.strip(),
94
+ "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
95
+ "answers": answers_payload
96
+ }
97
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
98
  print(status_update)
99
 
 
124
  results_df = pd.DataFrame(results_log)
125
  return status_message, results_df
126
 
 
127
  # Gradio Interface
128
  with gr.Blocks() as demo:
129
  gr.Markdown("# Basic Agent Evaluation Runner")