Neon-AI commited on
Commit
5ec3978
·
verified ·
1 Parent(s): 9f95ccf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -9
app.py CHANGED
@@ -1,11 +1,74 @@
1
- import subprocess
 
 
 
2
 
3
- url = "https://hlsx3cdn.echovideo.to/naruto/1/master.m3u8"
4
- output = "/tmp/video.mp4" # HF Spaces usually allow writing in /tmp
5
 
6
- subprocess.run([
7
- "ffmpeg",
8
- "-i", url,
9
- "-c", "copy",
10
- output
11
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from fastapi import FastAPI, HTTPException
3
+ from pydantic import BaseModel
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
 
6
+ MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
 
7
 
8
+ app = FastAPI(
9
+ title="General AI REST API",
10
+ version="1.0.0"
11
+ )
12
+
13
+ # Load once at startup (critical for speed)
14
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ MODEL_ID,
17
+ device_map="cpu",
18
+ torch_dtype=torch.float32
19
+ )
20
+ model.eval()
21
+
22
+ # ----------- Schemas -----------
23
+
24
+ class ChatRequest(BaseModel):
25
+ prompt: str
26
+ max_tokens: int = 120
27
+ temperature: float = 0.35
28
+ top_p: float = 0.9
29
+
30
+ class ChatResponse(BaseModel):
31
+ reply: str
32
+
33
+ # ----------- Endpoints -----------
34
+
35
+ @app.get("/health")
36
+ def health():
37
+ return {"status": "ok"}
38
+
39
+ @app.post("/chat", response_model=ChatResponse)
40
+ def chat(req: ChatRequest):
41
+ if not req.prompt or len(req.prompt.strip()) == 0:
42
+ raise HTTPException(status_code=400, detail="Prompt is empty")
43
+
44
+ # Hard safety limits (CPU protection)
45
+ prompt = req.prompt[:500]
46
+ max_tokens = min(req.max_tokens, 150)
47
+
48
+ messages = [
49
+ {"role": "system", "content": "You are a concise, intelligent assistant. Answer clearly and briefly."},
50
+ {"role": "user", "content": prompt}
51
+ ]
52
+
53
+ input_ids = tokenizer.apply_chat_template(
54
+ messages,
55
+ tokenize=True,
56
+ return_tensors="pt"
57
+ )
58
+
59
+ with torch.no_grad():
60
+ output = model.generate(
61
+ input_ids,
62
+ max_new_tokens=max_tokens,
63
+ temperature=req.temperature,
64
+ top_p=req.top_p,
65
+ repetition_penalty=1.1,
66
+ do_sample=True
67
+ )
68
+
69
+ reply = tokenizer.decode(
70
+ output[0][input_ids.shape[-1]:],
71
+ skip_special_tokens=True
72
+ ).strip()
73
+
74
+ return {"reply": reply}