File size: 1,125 Bytes
9472914 53f591c 9472914 | 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 | from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from langchain_community.llms import Ollama
import uvicorn
import asyncio
import os
import sys
app = FastAPI()
os.system("ollama serve &")
os.system("ollama pull hf.co/mradermacher/Llama-3.2-8X3B-MOE-Dark-Champion-Instruct-uncensored-abliterated-18.4B-i1-GGUF:IQ1_S")
# Initialize Ollama outside the endpoint (only once at startup)
llm = Ollama(model="hf.co/mradermacher/Llama-3.2-8X3B-MOE-Dark-Champion-Instruct-uncensored-abliterated-18.4B-i1-GGUF:IQ1_S")
class QueryRequest(BaseModel):
query: str
@app.post("/generate")
async def generate_text(request: QueryRequest):
"""
Endpoint to generate text using Ollama.
Receives a JSON payload with a 'query' field.
Returns:
dict: A JSON response containing the original query and the generated text.
"""
try:
result = await asyncio.to_thread(llm.invoke, request.query)
return {result}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |