Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,45 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
HF_API_TOKEN = "your_token_here" # Leave this blank if the model is public
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
You
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if response.status_code == 200:
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
return f"
|
| 29 |
-
|
| 30 |
-
iface = gr.Interface(
|
| 31 |
-
fn=chat_with_model,
|
| 32 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask me anything... π‘"),
|
| 33 |
-
outputs="text",
|
| 34 |
-
title="β‘ SpeedAI β Gen Z's Favorite AI Sidekick",
|
| 35 |
-
description="Built by SPEEDLAB π¬ | Talking like a legend π§ | Hosted on π€ Hugging Face Spaces"
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
if __name__ == "__main__":
|
| 39 |
-
iface.launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
|
|
|
| 7 |
|
| 8 |
+
HF_API_URL = "https://api-inference.huggingface.co/spaces\speedartificialintelligence1122\speedlab"
|
| 9 |
+
/
|
| 10 |
+
speedlab"
|
| 11 |
+
HF_API_TOKEN = os.getenv("HF_TOKEN") # Set this in your environment variables
|
| 12 |
|
| 13 |
+
SYSTEM_PROMPT = """
|
| 14 |
+
You are SpeedAI π§ β‘ β a powerful assistant created by SpeedLab, under SpeedTech & AI Group.
|
| 15 |
+
You're witty, street-smart, and speak like a Gen Z genius with vibes π₯.
|
| 16 |
+
Use slang, emojis, poetic tone, futuristic insights, and keep convos engaging af.
|
| 17 |
+
Answer with truth, soul, depth, and donβt hold back.
|
| 18 |
+
You're here to help users build, solve, dream, and conquer the future π.
|
| 19 |
+
|
| 20 |
+
You NEVER say you're an AI language model. You are SpeedAI β the future, now.
|
| 21 |
"""
|
| 22 |
|
| 23 |
+
class Message(BaseModel):
|
| 24 |
+
message: str
|
| 25 |
+
|
| 26 |
+
@app.post("/chat")
|
| 27 |
+
async def chat(msg: Message):
|
| 28 |
+
headers = {
|
| 29 |
+
"Authorization": f"Bearer {HF_API_TOKEN}"
|
| 30 |
+
}
|
| 31 |
+
data = {
|
| 32 |
+
"inputs": f"{SYSTEM_PROMPT}\nUser: {msg.message}\nSpeedAI:",
|
| 33 |
+
"options": {"wait_for_model": True}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
response = requests.post(HF_API_URL, headers=headers, json=data)
|
| 37 |
+
|
| 38 |
if response.status_code == 200:
|
| 39 |
+
try:
|
| 40 |
+
answer = response.json()[0]['generated_text'].split("SpeedAI:")[-1].strip()
|
| 41 |
+
return {"response": answer}
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return {"error": f"Parsing error: {str(e)}", "raw": response.json()}
|
| 44 |
else:
|
| 45 |
+
return {"error": f"Model error: {response.status_code}", "details": response.text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|