Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,67 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
# Load model
|
| 9 |
model_id = "HuggingFaceTB/SmolLM2-360M-Instruct"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
class ChatRequest(BaseModel):
|
| 13 |
message: str
|
| 14 |
|
|
|
|
| 15 |
@app.post("/chat")
|
| 16 |
async def chat(request: ChatRequest):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return {"response": response}
|
| 21 |
|
|
|
|
| 22 |
if __name__ == "__main__":
|
| 23 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Load model + tokenizer
|
| 9 |
model_id = "HuggingFaceTB/SmolLM2-360M-Instruct"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 12 |
+
|
| 13 |
+
chat_pipeline = pipeline(
|
| 14 |
+
"text-generation",
|
| 15 |
+
model=model,
|
| 16 |
+
tokenizer=tokenizer,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
SYSTEM_PROMPT = "You are Bella, a smart and witty female AI assistant. Keep responses short and to the point."
|
| 20 |
+
|
| 21 |
|
| 22 |
class ChatRequest(BaseModel):
|
| 23 |
message: str
|
| 24 |
|
| 25 |
+
|
| 26 |
@app.post("/chat")
|
| 27 |
async def chat(request: ChatRequest):
|
| 28 |
+
messages = [
|
| 29 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 30 |
+
{"role": "user", "content": request.message},
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# Build prompt using the model's chat template
|
| 34 |
+
prompt = tokenizer.apply_chat_template(
|
| 35 |
+
messages,
|
| 36 |
+
tokenize=False,
|
| 37 |
+
add_generation_prompt=True,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
outputs = chat_pipeline(
|
| 41 |
+
prompt,
|
| 42 |
+
max_new_tokens=175,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
temperature=0.3,
|
| 45 |
+
top_p=0.9,
|
| 46 |
+
repetition_penalty=1.2,
|
| 47 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
generated_text = outputs[0]["generated_text"]
|
| 51 |
+
|
| 52 |
+
# Strip off the prompt portion, leaving only the new reply
|
| 53 |
+
response = generated_text[len(prompt):].strip()
|
| 54 |
+
|
| 55 |
+
# Safety net: cut off if the model starts hallucinating a new turn
|
| 56 |
+
for stop_token in ["<|im_start|>", "User:", "system"]:
|
| 57 |
+
if stop_token in response:
|
| 58 |
+
response = response.split(stop_token)[0].strip()
|
| 59 |
+
|
| 60 |
+
if not response:
|
| 61 |
+
response = "Hmm, I didn't quite catch that — can you rephrase?"
|
| 62 |
+
|
| 63 |
return {"response": response}
|
| 64 |
|
| 65 |
+
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|