Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,42 +1,38 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from
|
|
|
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from rapidocr_onnxruntime import RapidOCR
|
| 5 |
-
import io
|
| 6 |
-
import numpy as np
|
| 7 |
-
import pandas as pd
|
| 8 |
|
| 9 |
-
model = RapidOCR()
|
| 10 |
app = FastAPI()
|
| 11 |
-
|
| 12 |
-
origins = [
|
| 13 |
-
"https://hycjack-fastapi-rapidocr.hf.space/",
|
| 14 |
-
"http://localhost",
|
| 15 |
-
"http://localhost:7860",
|
| 16 |
-
"http://127.0.0.1:7860"
|
| 17 |
-
]
|
| 18 |
|
| 19 |
app.add_middleware(
|
| 20 |
CORSMiddleware,
|
| 21 |
allow_origins=origins,
|
| 22 |
allow_credentials=True,
|
| 23 |
-
allow_methods=["*"],
|
| 24 |
-
allow_headers=["*"]
|
| 25 |
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=origins,
|
| 12 |
allow_credentials=True,
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
def chat_with_model(query: str, model: str) -> JSONResponse:
|
| 18 |
+
results = None
|
| 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) -> JSONResponse:
|
| 27 |
+
results = None
|
| 28 |
+
try:
|
| 29 |
+
return chat_with_model(query, model='gpt-4o-mini')
|
| 30 |
+
except Exception as e:
|
| 31 |
+
try:
|
| 32 |
+
return chat_with_model(query, model='claude-3-haiku')
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return JSONResponse(content={"error": str(e)})
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
import uvicorn
|
| 38 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|