Spaces:
Paused
Paused
File size: 1,512 Bytes
b690869 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain_huggingface import ChatHuggingFace, HuggingFacePipeline
import uvicorn
import os
# Define request model
class PromptRequest(BaseModel):
prompt: str
app = FastAPI(title="Local LLM Server")
# Global variable for model
model = None
@app.on_event("startup")
async def startup_event():
global model
print("Initializing TinyLlama... This will auto-download the model if not present.")
# Initialize the pipeline as requested
llm = HuggingFacePipeline.from_model_id(
model_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
task="text-generation",
pipeline_kwargs=dict(
temperature=0.7,
max_new_tokens=512,
do_sample=True
)
)
model = ChatHuggingFace(llm=llm)
print("Model loaded and ready to serve!")
@app.post("/generate")
async def generate_text(request: PromptRequest):
if not model:
raise HTTPException(status_code=503, detail="Model is loading, please wait...")
try:
# Invoke the model
result = model.invoke(request.prompt)
# Return the content of the AIMessage
return {"response": result.content}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
print("Starting server on http://127.0.0.1:8000")
uvicorn.run(app, host="127.0.0.1", port=8000)
|