deepak191z commited on
Commit
745d3f1
1 Parent(s): dd68089

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +28 -32
main.py CHANGED
@@ -1,42 +1,38 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from PIL import Image
 
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
- @app.get("/")
28
- def read_root():
29
- return {"Hello": "World!"}
30
-
31
- @app.post("/ocr")
32
- async def ocr(file: UploadFile = File(...)):
33
- contents = await file.read()
34
- image = Image.open(io.BytesIO(contents))
35
- np_array = np.array(image)
36
- ocr_result, elapse = model(np_array)
37
- dt_boxes, rec_res, scores = list(zip(*ocr_result))
38
- out_df = pd.DataFrame(
39
- [[box, rec, score] for box, rec, score in zip(dt_boxes, rec_res, scores)],
40
- columns=("box", "rec", "score"),
41
- )
42
- return out_df.to_dict(orient='records')
 
 
 
 
 
 
 
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")