speedartificialintelligence1122 commited on
Commit
f198b1b
·
verified ·
1 Parent(s): b82131d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -45
app.py CHANGED
@@ -1,66 +1,60 @@
 
1
  from transformers import AutoModelForCausalLM, AutoTokenizer
2
  import torch
3
- from fastapi import FastAPI, Request
4
  from pydantic import BaseModel
5
- from fastapi.middleware.cors import CORSMiddleware
6
 
7
- # Load model + tokenizer
8
  model_name = "https://huggingface.co/spaces/speedartificialintelligence1122/speedlab/edit/main/app.py"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name,token)
10
  model = AutoModelForCausalLM.from_pretrained(model_name)
11
 
12
- # App setup
13
- app = FastAPI()
14
-
15
- # CORS to allow frontend to talk to backend
16
- app.add_middleware(
17
- CORSMiddleware,
18
- allow_origins=["*"],
19
- allow_credentials=True,
20
- allow_methods=["*"],
21
- allow_headers=["*"],
22
  )
23
 
24
- # Request body
25
- class Message(BaseModel):
 
 
26
  prompt: str
27
- history: list = []
28
 
29
- # ✨ Poetic divine system prompt
30
- system_prompt = (
31
- "You are SpeedAI — an ethereal, poetic, and futuristic AI born to guide humanity into a new era. "
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 chat_with_speedai(msg: Message):
42
- # Build full prompt
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
- # Tokenize & generate
49
- input_ids = tokenizer.encode(full_prompt, return_tensors="pt")
50
- output_ids = model.generate(
51
- input_ids,
52
- max_length=500,
53
- pad_token_id=tokenizer.eos_token_id,
 
54
  do_sample=True,
55
- top_p=0.95,
56
  top_k=50,
57
- temperature=0.8
 
58
  )
59
 
60
- # Decode response
61
- output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
62
 
63
- # Extract only SpeedAI's final reply (cut after last "SpeedAI:")
64
- response = output_text.split("SpeedAI:")[-1].strip()
 
 
 
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)