Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines7891011121310 unmodified lines2425262728293031323334353637382 unmodified lines4142434445464748495051525354555657586 unmodified linesapp = FastAPI()model_name = "google/gemma-2b-it"tokenizer = AutoTokenizer.from_pretrained(model_name)10 unmodified lines@app.get("/")async def root():return {"status": "Gemma 2B is running"}@app.post("/")async def receive(data: Message):try:message = data.messageinputs = tokenizer(message,return_tensors="pt",truncation=True,max_length=2562 unmodified lineswith torch.no_grad():outputs = model.generate(**inputs,max_new_tokens=40,do_sample=False,pad_token_id=tokenizer.eos_token_id)reply = tokenizer.decode(outputs[0], skip_special_tokens=True)return JSONResponse({"reply": reply})except Exception as e:return JSONResponse({"error": str(e)})# 👇 ВАЖНЫЙ БЛОКif __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)6 unmodified lines7891011121310 unmodified lines2425262728293031323334353637383940412 unmodified lines44454647484950515253545556575859606162636 unmodified linesapp = FastAPI()model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"tokenizer = AutoTokenizer.from_pretrained(model_name)10 unmodified lines@app.get("/")async def root():return {"status": "TinyLlama is running"}@app.post("/")async def receive(data: Message):try:user_message = data.message# Правильный chat формат для TinyLlamaprompt = f"<|user|>\n{user_message}\n<|assistant|>\n"inputs = tokenizer(prompt,return_tensors="pt",truncation=True,max_length=2562 unmodified lineswith torch.no_grad():outputs = model.generate(**inputs,max_new_tokens=30, # ключ к скоростиdo_sample=False, # быстрееpad_token_id=tokenizer.eos_token_id)reply = tokenizer.decode(outputs[0], skip_special_tokens=True)# Убираем промпт из ответаreply = reply.split("<|assistant|>")[-1].strip()return JSONResponse({"reply": reply})except Exception as e:return JSONResponse({"error": str(e)})if __name__ == "__main__":uvicorn.run(app, host="0.0.0.0", port=7860)
app.py+12-7