Spaces:
Sleeping
Sleeping
Denys Kanunnikov
commited on
Commit
·
acef13a
1
Parent(s):
8e5831a
update agent logic
Browse files- agent.py +19 -3
- app.py +2 -2
- requirements.txt +3 -1
agent.py
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
class BaseAgent:
|
| 2 |
"""Base class for all agents. Override __call__ to implement agent logic."""
|
| 3 |
def __call__(self, question: str) -> str:
|
| 4 |
raise NotImplementedError("Agent must implement __call__ method.")
|
| 5 |
|
| 6 |
-
class
|
| 7 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def __call__(self, question: str) -> str:
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
class BaseAgent:
|
| 5 |
"""Base class for all agents. Override __call__ to implement agent logic."""
|
| 6 |
def __call__(self, question: str) -> str:
|
| 7 |
raise NotImplementedError("Agent must implement __call__ method.")
|
| 8 |
|
| 9 |
+
class LLMOpenAIAgent(BaseAgent):
|
| 10 |
+
"""Agent that uses OpenAI's GPT-3.5-turbo to answer questions."""
|
| 11 |
+
def __init__(self, api_key=None):
|
| 12 |
+
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
|
| 13 |
+
openai.api_key = self.api_key
|
| 14 |
+
|
| 15 |
def __call__(self, question: str) -> str:
|
| 16 |
+
try:
|
| 17 |
+
response = openai.ChatCompletion.create(
|
| 18 |
+
model="gpt-3.5-turbo",
|
| 19 |
+
messages=[{"role": "user", "content": question}],
|
| 20 |
+
max_tokens=256,
|
| 21 |
+
temperature=0.2,
|
| 22 |
+
)
|
| 23 |
+
return response.choices[0].message["content"].strip()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {e}"
|
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
-
from agent import
|
| 5 |
from api import APIClient
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
|
@@ -20,7 +20,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 20 |
return "Please Login to Hugging Face with the button.", None
|
| 21 |
|
| 22 |
api = APIClient(DEFAULT_API_URL)
|
| 23 |
-
agent =
|
| 24 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 25 |
|
| 26 |
# Fetch questions
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
+
from agent import LLMOpenAIAgent
|
| 5 |
from api import APIClient
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
|
|
|
| 20 |
return "Please Login to Hugging Face with the button.", None
|
| 21 |
|
| 22 |
api = APIClient(DEFAULT_API_URL)
|
| 23 |
+
agent = LLMOpenAIAgent()
|
| 24 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 25 |
|
| 26 |
# Fetch questions
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
| 3 |
+
openai
|
| 4 |
+
transformers
|