Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,45 +10,54 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
import re
|
| 15 |
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
-
print("Hybrid Agent
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def classify(self, question: str) -> str:
|
| 22 |
q = question.lower()
|
| 23 |
-
if any(
|
| 24 |
return "media"
|
| 25 |
-
if any(
|
| 26 |
return "logic"
|
| 27 |
-
return "
|
| 28 |
|
| 29 |
-
def handle_media(self,
|
| 30 |
return "I'm unable to process audio, video, or file-based questions."
|
| 31 |
|
| 32 |
-
def handle_logic(self,
|
| 33 |
-
q =
|
| 34 |
-
|
| 35 |
-
# Example: subset + commutative detection
|
| 36 |
if "not commutative" in q and "subset" in q:
|
| 37 |
return "a,b,c"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def __call__(self, question: str) -> str:
|
| 54 |
qtype = self.classify(question)
|
|
@@ -59,7 +68,8 @@ class BasicAgent:
|
|
| 59 |
elif qtype == "logic":
|
| 60 |
return self.handle_logic(question)
|
| 61 |
else:
|
| 62 |
-
return self.
|
|
|
|
| 63 |
|
| 64 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 65 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
# ---------- MODIFICATIONS BEGIN ----------
|
| 14 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 15 |
+
import torch
|
| 16 |
+
import os
|
| 17 |
import re
|
| 18 |
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
+
print("Hybrid Agent with Mistral Model Initialized")
|
| 22 |
+
|
| 23 |
+
model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 24 |
+
|
| 25 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_id, token=os.getenv("HF_NEW_API_TOKEN"))
|
| 26 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_id, token=os.getenv("HF_NEW_API_TOKEN"))
|
| 27 |
+
self.model.to("cpu")
|
| 28 |
+
self.model.eval()
|
| 29 |
|
| 30 |
def classify(self, question: str) -> str:
|
| 31 |
q = question.lower()
|
| 32 |
+
if any(x in q for x in ["youtube", ".mp3", "image", "video", "attached", ".wav"]):
|
| 33 |
return "media"
|
| 34 |
+
if any(x in q for x in ["|", "*", "subset", "commutative", "table", "="]):
|
| 35 |
return "logic"
|
| 36 |
+
return "mistral"
|
| 37 |
|
| 38 |
+
def handle_media(self, q: str) -> str:
|
| 39 |
return "I'm unable to process audio, video, or file-based questions."
|
| 40 |
|
| 41 |
+
def handle_logic(self, q: str) -> str:
|
| 42 |
+
q = q.lower()
|
|
|
|
|
|
|
| 43 |
if "not commutative" in q and "subset" in q:
|
| 44 |
return "a,b,c"
|
| 45 |
+
return "I couldn't solve this logic-based question."
|
| 46 |
+
|
| 47 |
+
def handle_mistral(self, question: str) -> str:
|
| 48 |
+
prompt = f"<s>[INST] {question.strip()} [/INST]"
|
| 49 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 50 |
+
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = self.model.generate(
|
| 53 |
+
**inputs,
|
| 54 |
+
max_new_tokens=256,
|
| 55 |
+
do_sample=True,
|
| 56 |
+
temperature=0.7,
|
| 57 |
+
top_p=0.95
|
| 58 |
+
)
|
| 59 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 60 |
+
return response.split("[/INST]")[-1].strip()
|
| 61 |
|
| 62 |
def __call__(self, question: str) -> str:
|
| 63 |
qtype = self.classify(question)
|
|
|
|
| 68 |
elif qtype == "logic":
|
| 69 |
return self.handle_logic(question)
|
| 70 |
else:
|
| 71 |
+
return self.handle_mistral(question)
|
| 72 |
+
# ---------- MODIFICATIONS END ----------
|
| 73 |
|
| 74 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 75 |
"""
|