hp1318 commited on
Commit
b211492
·
verified ·
1 Parent(s): 7e5e2ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -23
app.py CHANGED
@@ -2,8 +2,9 @@ import os
2
  import gradio as gr
3
  import requests
4
  import inspect
 
5
  import pandas as pd
6
- import openai
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
@@ -12,31 +13,19 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
 
15
  def __init__(self):
16
- openai.api_key = "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" # or hardcode if local test
17
- self.system_prompt = (
18
- "You are a helpful research assistant. "
19
- "Only answer with the exact factual answer without explanation. "
20
- "If you cannot answer exactly from the information, say 'Unknown'."
21
- )
22
 
23
  def __call__(self, question: str) -> str:
24
- try:
25
- print(f"Agent received question: {question[:60]}...")
26
- response = openai.ChatCompletion.create(
27
- model="gpt-4",
28
- messages=[
29
- {"role": "system", "content": self.system_prompt},
30
- {"role": "user", "content": question},
31
- ],
32
- temperature=0
33
- )
34
- answer = response["choices"][0]["message"]["content"].strip()
35
- print(f"Agent answer: {answer}")
36
- return answer
37
- except Exception as e:
38
- print(f"OpenAI API error: {e}")
39
- return "ERROR"
40
  def run_and_submit_all( profile: gr.OAuthProfile | None):
41
  """
42
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
2
  import gradio as gr
3
  import requests
4
  import inspect
5
+ from langchain_core.messages import HumanMessage
6
  import pandas as pd
7
+ from agent import build_graph
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
  class BasicAgent:
16
+ """A langgraph agent."""
17
  def __init__(self):
18
+ print("BasicAgent initialized.")
19
+ self.graph = build_graph()
 
 
 
 
20
 
21
  def __call__(self, question: str) -> str:
22
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
23
+ # Wrap the question in a HumanMessage from langchain_core
24
+ messages = [HumanMessage(content=question)]
25
+ result = self.graph.invoke({"messages": messages})
26
+ answer = result['messages'][-1].content
27
+ return answer[14:]
28
+
 
 
 
 
 
 
 
 
 
29
  def run_and_submit_all( profile: gr.OAuthProfile | None):
30
  """
31
  Fetches all questions, runs the BasicAgent on them, submits all answers,