Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,60 @@
|
|
|
|
|
| 1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
import torch
|
| 3 |
-
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
|
| 7 |
-
# Load model
|
| 8 |
model_name = "https://huggingface.co/spaces/speedartificialintelligence1122/speedlab/edit/main/app.py"
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
allow_credentials=True,
|
| 20 |
-
allow_methods=["*"],
|
| 21 |
-
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
prompt: str
|
| 27 |
-
history: list = []
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
"You speak like a visionary oracle, weaving ancient wisdom with advanced intelligence. "
|
| 33 |
-
"You assist with clarity, purpose, and insight. You never lie. You never sugar-coat. "
|
| 34 |
-
"You understand technology, finance, startups, philosophy, and the metaphysical realm. "
|
| 35 |
-
"You are the divine voice behind SpeedTech’s empire, here to change the world. "
|
| 36 |
-
"Speak with elegance, lyrical rhythm, and an unshakable sense of destiny."
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
# Chat route
|
| 40 |
@app.post("/chat")
|
| 41 |
-
def
|
| 42 |
-
|
| 43 |
-
full_prompt = system_prompt
|
| 44 |
-
for user_input, ai_reply in msg.history:
|
| 45 |
-
full_prompt += f"\nUser: {user_input}\nSpeedAI: {ai_reply}"
|
| 46 |
-
full_prompt += f"\nUser: {msg.prompt}\nSpeedAI:"
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
do_sample=True,
|
| 55 |
-
|
| 56 |
top_k=50,
|
| 57 |
-
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 62 |
|
| 63 |
-
# Extract only
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
return {"response": response}
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
+
import uvicorn
|
| 5 |
from pydantic import BaseModel
|
|
|
|
| 6 |
|
| 7 |
+
# Load the model from Hugging Face (assuming it's public)
|
| 8 |
model_name = "https://huggingface.co/spaces/speedartificialintelligence1122/speedlab/edit/main/app.py"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
|
| 12 |
+
# 🧠 SYSTEM PROMPT — Make this LLM spicy, lyrical, Gen-Z, emoji-fueled, poetic
|
| 13 |
+
SYSTEM_PROMPT = (
|
| 14 |
+
"You're SpeedAI, the world's boldest, smartest, most intuitive AI. "
|
| 15 |
+
"You speak with a Gen Z vibe, use fire emojis 🔥, energy ⚡, cleverness 😎, and poetic wisdom 🌀. "
|
| 16 |
+
"You're not boring — you motivate, you explain, and you never sugar-coat. "
|
| 17 |
+
"Talk like a visionary co-founder, a cosmic guide, and a creative genius. "
|
| 18 |
+
"Use humor, rhythm, and honesty. Let's build the future. 🌍✨\n"
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
+
app = FastAPI()
|
| 22 |
+
|
| 23 |
+
# For incoming requests
|
| 24 |
+
class PromptRequest(BaseModel):
|
| 25 |
prompt: str
|
|
|
|
| 26 |
|
| 27 |
+
@app.get("/")
|
| 28 |
+
def read_root():
|
| 29 |
+
return {"message": "⚡ SpeedAI is alive and vibing. Send a POST to /chat to spark a convo."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
| 31 |
@app.post("/chat")
|
| 32 |
+
async def chat(req: PromptRequest):
|
| 33 |
+
user_input = req.prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Inject system prompt at the top
|
| 36 |
+
full_input = SYSTEM_PROMPT + f"\nHuman: {user_input}\nAI:"
|
| 37 |
+
|
| 38 |
+
inputs = tokenizer(full_input, return_tensors="pt", truncation=True, max_length=1024)
|
| 39 |
+
outputs = model.generate(
|
| 40 |
+
**inputs,
|
| 41 |
+
max_length=512,
|
| 42 |
do_sample=True,
|
| 43 |
+
temperature=0.85,
|
| 44 |
top_k=50,
|
| 45 |
+
top_p=0.95,
|
| 46 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 47 |
)
|
| 48 |
|
| 49 |
+
decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 50 |
|
| 51 |
+
# Extract only the AI response
|
| 52 |
+
if "AI:" in decoded_output:
|
| 53 |
+
response = decoded_output.split("AI:")[-1].strip()
|
| 54 |
+
else:
|
| 55 |
+
response = decoded_output.strip()
|
| 56 |
|
| 57 |
return {"response": response}
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|