Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,37 +1,55 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from duckduckgo_search import DDGS
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
-
origins = ["*"]
|
| 8 |
|
|
|
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
| 11 |
-
allow_origins=
|
| 12 |
allow_credentials=True,
|
| 13 |
-
allow_methods=["
|
| 14 |
-
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
try:
|
| 20 |
-
results = DDGS().chat(query, model=model)
|
| 21 |
-
except Exception as e:
|
| 22 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 23 |
-
return JSONResponse(content={"results": results})
|
| 24 |
|
| 25 |
@app.get("/chat/")
|
| 26 |
-
async def chat(query: str)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
import uvicorn
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
|
|
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from duckai import DuckAI
|
| 5 |
|
| 6 |
app = FastAPI()
|
|
|
|
| 7 |
|
| 8 |
+
# Configure CORS
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
allow_credentials=True,
|
| 13 |
+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
class ChatQuery(BaseModel):
|
| 18 |
+
query: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@app.get("/chat/")
|
| 21 |
+
async def chat(query: str):
|
| 22 |
+
if not query:
|
| 23 |
+
raise HTTPException(status_code=400, detail="Query parameter is required")
|
| 24 |
+
|
| 25 |
+
duck = DuckAI()
|
| 26 |
try:
|
| 27 |
+
results = duck.chat(query, model='gpt-4o-mini')
|
| 28 |
+
return {"results": results}
|
| 29 |
+
except Exception as e1:
|
| 30 |
+
print(f"Primary model (gpt-4o-mini) failed: {e1}")
|
| 31 |
try:
|
| 32 |
+
results = duck.chat(query, model='claude-3-haiku')
|
| 33 |
+
return {"results": results}
|
| 34 |
+
except Exception as e2:
|
| 35 |
+
print(f"Fallback model (claude-3-haiku) also failed: {e2}")
|
| 36 |
+
raise HTTPException(
|
| 37 |
+
status_code=500,
|
| 38 |
+
detail={
|
| 39 |
+
"error": "Both models failed",
|
| 40 |
+
"primary_error": str(e1),
|
| 41 |
+
"fallback_error": str(e2)
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
async def chat_with_model(query: str, model: str):
|
| 46 |
+
try:
|
| 47 |
+
duck = DuckAI()
|
| 48 |
+
results = duck.chat(query, model=model)
|
| 49 |
+
return {"results": results}
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
+
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
import uvicorn
|