Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,27 +23,31 @@ class BasicAgent:
|
|
| 23 |
|
| 24 |
class ZephyrAgent:
|
| 25 |
def __init__(self):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 49 |
"""
|
|
|
|
| 23 |
|
| 24 |
class ZephyrAgent:
|
| 25 |
def __init__(self):
|
| 26 |
+
self.api_url = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
| 27 |
+
self.headers = {
|
| 28 |
+
"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"
|
| 29 |
+
}
|
| 30 |
+
print("ZephyrAPI initialized using Inference API.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def __call__(self, question: str) -> str:
|
| 33 |
+
prompt = f"<|system|>\nYou are a helpful assistant.\n<|user|>\n{question}\n<|assistant|>\n"
|
| 34 |
+
payload = {
|
| 35 |
+
"inputs": prompt,
|
| 36 |
+
"parameters": {
|
| 37 |
+
"max_new_tokens": 256,
|
| 38 |
+
"temperature": 0.7,
|
| 39 |
+
"top_p": 0.9,
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(self.api_url, headers=self.headers, json=payload, timeout=60)
|
| 45 |
+
response.raise_for_status()
|
| 46 |
+
result = response.json()
|
| 47 |
+
return result[0]["generated_text"].split("<|assistant|>")[-1].strip()
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error: {e}")
|
| 50 |
+
return "⚠️ Model could not respond. Check API access or token."
|
| 51 |
|
| 52 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 53 |
"""
|