Mazenbs commited on
Commit
cda9042
·
verified ·
1 Parent(s): fb71cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -47
app.py CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  from fastapi.middleware.cors import CORSMiddleware
5
- from embeddingonnx import text_to_embedding, query_to_embedding
6
 
7
- app = FastAPI(title="Arabic Text Embedding API")
 
8
 
9
- # ------------- CORS -------------
10
  app.add_middleware(
11
  CORSMiddleware,
12
  allow_origins=["*"],
@@ -15,68 +15,29 @@ app.add_middleware(
15
  allow_headers=["*"],
16
  )
17
 
18
- # ----------- Models ----------
19
  class TextRequest(BaseModel):
20
  text: str
21
 
22
-
23
  @app.get("/")
24
  def root():
25
- return {"message": "Arabic Embedding API is running on HF Spaces ✔"}
26
-
27
-
28
- @app.get("/health")
29
- def health():
30
- return {"status": "ok"}
31
-
32
 
33
  @app.post("/embed")
34
  def embed_post(req: TextRequest):
35
  text = req.text.strip()
36
  if not text:
37
  raise HTTPException(400, "النص فارغ")
38
-
39
  vec = text_to_embedding(text, normalize=True)
40
  return {"embedding": vec.tolist()}
41
 
42
-
43
- @app.post("/query")
44
- def query_post(req: TextRequest):
45
- text = req.text.strip()
46
- if not text:
47
- raise HTTPException(400, "النص فارغ")
48
-
49
- vec = query_to_embedding(text, normalize=True)
50
- return {"query_embedding": vec.tolist()}
51
-
52
-
53
- # ------ GET Versions ------
54
- @app.get("/embed")
55
- def embed_get(text: str):
56
- text = text.strip()
57
- if not text:
58
- raise HTTPException(400, "النص فارغ")
59
- vec = text_to_embedding(text, normalize=True)
60
- return {"embedding": vec.tolist()}
61
-
62
-
63
- @app.get("/query")
64
- def query_get(text: str):
65
- text = text.strip()
66
- if not text:
67
- raise HTTPException(400, "النص فارغ")
68
- vec = query_to_embedding(text, normalize=True)
69
- return {"query_embedding": vec.tolist()}
70
-
71
-
72
- # ========== Gradio UI ==========
73
- def ui_fn(text):
74
  if not text.strip():
75
  return "❌ النص فارغ"
76
  vec = text_to_embedding(text, normalize=True)
77
  return str(vec.tolist())
78
 
 
79
 
80
- demo = gr.Interface(fn=ui_fn, inputs="text", outputs="text", title="Arabic Embedding API")
81
-
82
  gr.mount_gradio_app(app, demo, path="/gradio")
 
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ from embeddingonnx import text_to_embedding
6
 
7
+ # -------- FastAPI --------
8
+ app = FastAPI(title="Arabic Embedding API")
9
 
 
10
  app.add_middleware(
11
  CORSMiddleware,
12
  allow_origins=["*"],
 
15
  allow_headers=["*"],
16
  )
17
 
 
18
  class TextRequest(BaseModel):
19
  text: str
20
 
 
21
  @app.get("/")
22
  def root():
23
+ return {"message": "Arabic Embedding API is running ✔"}
 
 
 
 
 
 
24
 
25
  @app.post("/embed")
26
  def embed_post(req: TextRequest):
27
  text = req.text.strip()
28
  if not text:
29
  raise HTTPException(400, "النص فارغ")
 
30
  vec = text_to_embedding(text, normalize=True)
31
  return {"embedding": vec.tolist()}
32
 
33
+ # -------- Gradio UI --------
34
+ def gr_ui(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  if not text.strip():
36
  return "❌ النص فارغ"
37
  vec = text_to_embedding(text, normalize=True)
38
  return str(vec.tolist())
39
 
40
+ demo = gr.Interface(fn=gr_ui, inputs="text", outputs="text", title="Arabic Embedding API")
41
 
42
+ # ربط Gradio مع FastAPI
 
43
  gr.mount_gradio_app(app, demo, path="/gradio")