Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -19,6 +21,30 @@ class BasicAgent:
|
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
return fixed_answer
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -179,7 +205,8 @@ with gr.Blocks() as demo:
|
|
| 179 |
test_button = gr.Button("Test Agent")
|
| 180 |
|
| 181 |
def test_agent_response(question: str) -> str:
|
| 182 |
-
agent = BasicAgent()
|
|
|
|
| 183 |
return agent(question)
|
| 184 |
|
| 185 |
test_button.click(fn=test_agent_response, inputs=question_input, outputs=answer_output)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 7 |
+
import torch
|
| 8 |
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
|
|
|
| 21 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 22 |
return fixed_answer
|
| 23 |
|
| 24 |
+
class ZephyrAgent:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
| 27 |
+
print(f"Loading model: {model_id}")
|
| 28 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 29 |
+
self.pipeline = pipeline(
|
| 30 |
+
"text-generation",
|
| 31 |
+
model=model_id,
|
| 32 |
+
tokenizer=self.tokenizer,
|
| 33 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 34 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 35 |
+
max_new_tokens=512,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
top_p=0.9,
|
| 38 |
+
)
|
| 39 |
+
print("✅ ZephyrAgent initialized.")
|
| 40 |
+
|
| 41 |
+
def __call__(self, question: str) -> str:
|
| 42 |
+
prompt = f"<|system|>\nYou are a helpful AI assistant.\n<|user|>\n{question}\n<|assistant|>\n"
|
| 43 |
+
print(f"🧠 Prompting ZephyrAgent:\n{prompt[:100]}...")
|
| 44 |
+
response = self.pipeline(prompt, return_full_text=False)
|
| 45 |
+
generated_text = response[0]['generated_text'].strip()
|
| 46 |
+
return generated_text
|
| 47 |
+
|
| 48 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 49 |
"""
|
| 50 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 205 |
test_button = gr.Button("Test Agent")
|
| 206 |
|
| 207 |
def test_agent_response(question: str) -> str:
|
| 208 |
+
# agent = BasicAgent()
|
| 209 |
+
agent = ZephyrAgent()
|
| 210 |
return agent(question)
|
| 211 |
|
| 212 |
test_button.click(fn=test_agent_response, inputs=question_input, outputs=answer_output)
|