jan01 commited on
Commit
6eb33d1
·
verified ·
1 Parent(s): 49aa98c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -2,26 +2,39 @@ 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
  # --- Basic Agent Definition ---
11
- # Modified to use HF transformers pipeline for Q&A
12
  class BasicAgent:
13
  def __init__(self):
14
- print("BasicAgent initialized.")
15
- # Load the model pipeline for text2text-generation (question answering)
16
- self.qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-small")
 
 
17
 
18
  def __call__(self, question: str) -> str:
19
  print(f"Agent received question (first 50 chars): {question[:50]}...")
20
- # Run the question through the pipeline to get answer
21
- output = self.qa_pipeline(question, max_length=64)
22
- answer = output[0]['generated_text']
23
- print(f"Agent returning answer: {answer}")
24
- return answer
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def run_and_submit_all(profile: gr.OAuthProfile | None):
27
  """
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ import openai # NEW: import OpenAI SDK
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  # --- Basic Agent Definition ---
11
+ # Modified to use OpenAI GPT-4 API for Q&A
12
  class BasicAgent:
13
  def __init__(self):
14
+ self.api_key = os.getenv("OPENAI_API_KEY")
15
+ if not self.api_key:
16
+ raise ValueError("OPENAI_API_KEY environment variable is not set!")
17
+ openai.api_key = self.api_key
18
+ print("BasicAgent initialized with OpenAI API key.")
19
 
20
  def __call__(self, question: str) -> str:
21
  print(f"Agent received question (first 50 chars): {question[:50]}...")
22
+ try:
23
+ response = openai.ChatCompletion.create(
24
+ model="gpt-4",
25
+ messages=[
26
+ {"role": "system", "content": "You are a helpful assistant."},
27
+ {"role": "user", "content": question}
28
+ ],
29
+ max_tokens=200,
30
+ temperature=0
31
+ )
32
+ answer = response['choices'][0]['message']['content'].strip()
33
+ print(f"Agent returning answer: {answer}")
34
+ return answer
35
+ except Exception as e:
36
+ print(f"Error calling OpenAI API: {e}")
37
+ return "Sorry, I couldn't answer that."
38
 
39
  def run_and_submit_all(profile: gr.OAuthProfile | None):
40
  """