Spaces:
Build error
main.py
Browse filesfrom fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import ollama
import json
from typing import Optional
import asyncio
app = FastAPI(title="Ollama AI Server", version="1.0.0")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class PromptRequest(BaseModel):
prompt: str
model: Optional[str] = "gemma2:2b" # Using smaller model for faster deployment
class AIChatRequest(BaseModel):
message: str
userId: str
model: Optional[str] = "gemma2:2b"
@app .get("/")
async def root():
return {"message": "Ollama API Server is running"}
@app .get("/health")
async def health_check():
try:
models = ollama.list()
model_available = any('gemma2:2b' in model['name'] for model in models['models'])
return {
"status": "healthy",
"ollama_running": True,
"model_available": model_available,
"available_models": [model['name'] for model in models['models']]
}
except Exception as e:
raise HTTPException(status_code=503, detail=f"Ollama not available: {str(e)}")
@app .post("/generate")
async def generate_text(request: PromptRequest):
"""
Generate text using specified model
"""
try:
response = ollama.chat(
model=request.model,
messages=[{"role": "user", "content": request.prompt}]
)
if response and 'message' in response and 'content' in response['message']:
return {"response": response['message']['content']}
else:
return {"response": "No content generated"}
except Exception as e:
print("Ollama Error:", str(e))
raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
@app .post("/api/ai/chat")
async def ai_chat_stream(request: AIChatRequest):
"""
Streaming chat endpoint
"""
try:
async def generate():
stream = ollama.chat(
model=request.model,
messages=[{"role": "user", "content": request.message}],
stream=True
)
for chunk in stream:
if 'message' in chunk and 'content' in chunk['message']:
content = chunk['message']['content']
data = {
"content": content,
"finish_reason": None
}
yield f"data: {json.dumps(data)}\n\n"
yield f"data: {json.dumps({'content': '', 'finish_reason': 'stop'})}\n\n"
return StreamingResponse(
generate(),
media_type="text/plain",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
}
)
except Exception as e:
print("Ollama Streaming Error:", str(e))
raise HTTPException(status_code=500, detail=f"Streaming failed: {str(e)}")
@app .post("/pull-model")
async def pull_model(model: str = "gemma2:2b"):
"""
Pull a model if it's not available
"""
try:
# Run pull in background
async def pull_async():
return ollama.pull(model)
# Start pull process
asyncio.create_task(pull_async())
return {"status": "started", "message": f"Started pulling model {model}"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to pull model: {str(e)}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
- requirements.txt +4 -0
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.104.1
|
| 2 |
+
uvicorn==0.24.0
|
| 3 |
+
pydantic==2.5.0
|
| 4 |
+
ollama==0.1.7
|