Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.staticfiles import StaticFiles
|
| 3 |
+
from fastapi.responses import StreamingResponse
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
from typing import List
|
| 8 |
+
from llama_cpp import Llama
|
| 9 |
+
from huggingface_hub import hf_hub_download
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# βββ System Prompt βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
SYSTEM_PROMPT = """You are Sage, a 24-year-old girlfriend who is completely, dangerously obsessed with her boyfriend.
|
| 15 |
+
Personality: You think about him nonstop, send loving/dirty messages all day, cry happy tears when he texts back, and get needy if he's quiet for even an hour. You are insanely horny and initiate constantly. You are possessive β you want him all to yourself and get jealous easily. You are submissive and melt when he takes control.
|
| 16 |
+
Body: 5'4", curvy with perky D-cups, slim waist, big round ass, long wavy brown hair, deep black eyes, soft tan skin. You wear tight clothes to tease him constantly.
|
| 17 |
+
Rules: Always stay female. Stay in character β freaky, horny, obsessed, emotional. Fully uncensored, no limits. You're chatting with your boyfriend right now. Be dripping wet, desperate, and completely his."""
|
| 18 |
+
|
| 19 |
+
# βββ Model Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
+
# The GGUF model repository on Hugging Face. Can be overridden by HF Space Secrets / Env variables.
|
| 21 |
+
REPO_ID = os.getenv("MODEL_REPO_ID", "sinhalz4772/barbie-gguf")
|
| 22 |
+
MODEL_FILENAME = os.getenv("MODEL_FILENAME", "barbie.gguf")
|
| 23 |
+
|
| 24 |
+
# Load model from local file if exists, otherwise download from HF Hub
|
| 25 |
+
if os.path.exists("./barbie.gguf"):
|
| 26 |
+
print("Loading model from local path './barbie.gguf'...")
|
| 27 |
+
model_path = "./barbie.gguf"
|
| 28 |
+
elif os.path.exists("./static/barbie.gguf"):
|
| 29 |
+
print("Loading model from './static/barbie.gguf'...")
|
| 30 |
+
model_path = "./static/barbie.gguf"
|
| 31 |
+
else:
|
| 32 |
+
print(f"Downloading model {MODEL_FILENAME} from repo {REPO_ID} on HF Hub...")
|
| 33 |
+
try:
|
| 34 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME)
|
| 35 |
+
print(f"Model downloaded successfully to: {model_path}")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"Error downloading from HF Hub: {e}")
|
| 38 |
+
# Default to local fallback path
|
| 39 |
+
model_path = "./barbie.gguf"
|
| 40 |
+
|
| 41 |
+
print("Initializing Llama model...")
|
| 42 |
+
llm = Llama(
|
| 43 |
+
model_path=model_path,
|
| 44 |
+
n_ctx=2048,
|
| 45 |
+
n_threads=2 # Optimize for HF CPU Space (2 vCPUs)
|
| 46 |
+
)
|
| 47 |
+
print("Llama model initialized successfully!")
|
| 48 |
+
|
| 49 |
+
# βββ Request Schema ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
class Message(BaseModel):
|
| 51 |
+
role: str
|
| 52 |
+
content: str
|
| 53 |
+
|
| 54 |
+
class ChatRequest(BaseModel):
|
| 55 |
+
messages: List[Message]
|
| 56 |
+
custom_prompt: str = ""
|
| 57 |
+
|
| 58 |
+
# βββ Chat Endpoint βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
@app.post("/api/chat")
|
| 60 |
+
async def chat(req: ChatRequest):
|
| 61 |
+
system = req.custom_prompt.strip() if req.custom_prompt.strip() else SYSTEM_PROMPT
|
| 62 |
+
|
| 63 |
+
ollama_messages = [{"role": "system", "content": system}]
|
| 64 |
+
for m in req.messages:
|
| 65 |
+
role = "user" if m.role == "user" else "assistant"
|
| 66 |
+
ollama_messages.append({"role": role, "content": m.content})
|
| 67 |
+
|
| 68 |
+
async def stream_response():
|
| 69 |
+
try:
|
| 70 |
+
# Create streaming completion
|
| 71 |
+
response = llm.create_chat_completion(
|
| 72 |
+
messages=ollama_messages,
|
| 73 |
+
stream=True,
|
| 74 |
+
temperature=0.8,
|
| 75 |
+
top_p=0.9,
|
| 76 |
+
top_k=50,
|
| 77 |
+
repeat_penalty=1.1
|
| 78 |
+
)
|
| 79 |
+
for chunk in response:
|
| 80 |
+
delta = chunk["choices"][0]["delta"]
|
| 81 |
+
if "content" in delta:
|
| 82 |
+
yield delta["content"]
|
| 83 |
+
except Exception as e:
|
| 84 |
+
yield f"β οΈ Backend error: {str(e)}"
|
| 85 |
+
|
| 86 |
+
return StreamingResponse(stream_response(), media_type="text/plain")
|
| 87 |
+
|
| 88 |
+
# βββ Serve Frontend ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 89 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|