Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
from
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
|
@@ -14,15 +14,11 @@ class BasicAgent:
|
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
self.model = OpenAIServerModel(
|
| 20 |
-
model_id="llama-3.1-8b-instant",
|
| 21 |
-
api_base="https://api.groq.com/openai/v1",
|
| 22 |
api_key=os.getenv("GROQ_API_KEY")
|
| 23 |
)
|
| 24 |
|
| 25 |
-
print("
|
| 26 |
|
| 27 |
def clean_answer(self, text):
|
| 28 |
|
|
@@ -43,31 +39,37 @@ class BasicAgent:
|
|
| 43 |
|
| 44 |
def __call__(self, question: str) -> str:
|
| 45 |
|
| 46 |
-
prompt = f"""
|
| 47 |
-
Answer the question accurately.
|
| 48 |
-
|
| 49 |
-
IMPORTANT:
|
| 50 |
-
- Return ONLY the final answer.
|
| 51 |
-
- No reasoning.
|
| 52 |
-
- No explanations.
|
| 53 |
-
- Keep answers concise.
|
| 54 |
-
|
| 55 |
-
Question:
|
| 56 |
-
{question}
|
| 57 |
-
"""
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
max_tokens=64
|
| 64 |
)
|
| 65 |
|
| 66 |
-
answer =
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
print(
|
|
|
|
| 69 |
|
| 70 |
-
return
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
|
|
@@ -77,7 +79,7 @@ Question:
|
|
| 77 |
|
| 78 |
|
| 79 |
# ---------------------------------------------------
|
| 80 |
-
# MAIN
|
| 81 |
# ---------------------------------------------------
|
| 82 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 83 |
|
|
@@ -91,14 +93,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 91 |
|
| 92 |
# init agent
|
| 93 |
try:
|
|
|
|
| 94 |
agent = BasicAgent()
|
|
|
|
| 95 |
except Exception as e:
|
|
|
|
| 96 |
return f"Agent init error: {e}", None
|
| 97 |
|
| 98 |
# fetch questions
|
| 99 |
try:
|
| 100 |
|
| 101 |
-
response = requests.get(
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
questions_data = response.json()
|
| 104 |
|
|
@@ -197,6 +205,4 @@ with gr.Blocks() as demo:
|
|
| 197 |
# ---------------------------------------------------
|
| 198 |
if __name__ == "__main__":
|
| 199 |
|
| 200 |
-
print("Launching app...")
|
| 201 |
-
|
| 202 |
demo.launch()
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
from groq import Groq
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
|
|
|
| 14 |
|
| 15 |
def __init__(self):
|
| 16 |
|
| 17 |
+
self.client = Groq(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
api_key=os.getenv("GROQ_API_KEY")
|
| 19 |
)
|
| 20 |
|
| 21 |
+
print("Groq client initialized.")
|
| 22 |
|
| 23 |
def clean_answer(self, text):
|
| 24 |
|
|
|
|
| 39 |
|
| 40 |
def __call__(self, question: str) -> str:
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
try:
|
| 43 |
|
| 44 |
+
completion = self.client.chat.completions.create(
|
| 45 |
+
model="llama-3.1-8b-instant",
|
| 46 |
+
messages=[
|
| 47 |
+
{
|
| 48 |
+
"role": "system",
|
| 49 |
+
"content": (
|
| 50 |
+
"You are solving benchmark questions. "
|
| 51 |
+
"Return ONLY the final answer. "
|
| 52 |
+
"No explanations. "
|
| 53 |
+
"Be concise."
|
| 54 |
+
)
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"role": "user",
|
| 58 |
+
"content": question
|
| 59 |
+
}
|
| 60 |
+
],
|
| 61 |
+
temperature=0,
|
| 62 |
max_tokens=64
|
| 63 |
)
|
| 64 |
|
| 65 |
+
answer = completion.choices[0].message.content
|
| 66 |
+
|
| 67 |
+
cleaned = self.clean_answer(answer)
|
| 68 |
|
| 69 |
+
print(f"\nQ: {question}")
|
| 70 |
+
print(f"A: {cleaned}")
|
| 71 |
|
| 72 |
+
return cleaned
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
# ---------------------------------------------------
|
| 82 |
+
# MAIN
|
| 83 |
# ---------------------------------------------------
|
| 84 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 85 |
|
|
|
|
| 93 |
|
| 94 |
# init agent
|
| 95 |
try:
|
| 96 |
+
|
| 97 |
agent = BasicAgent()
|
| 98 |
+
|
| 99 |
except Exception as e:
|
| 100 |
+
|
| 101 |
return f"Agent init error: {e}", None
|
| 102 |
|
| 103 |
# fetch questions
|
| 104 |
try:
|
| 105 |
|
| 106 |
+
response = requests.get(
|
| 107 |
+
questions_url,
|
| 108 |
+
timeout=30
|
| 109 |
+
)
|
| 110 |
|
| 111 |
questions_data = response.json()
|
| 112 |
|
|
|
|
| 205 |
# ---------------------------------------------------
|
| 206 |
if __name__ == "__main__":
|
| 207 |
|
|
|
|
|
|
|
| 208 |
demo.launch()
|