Mazenbs commited on
Commit
1fbc71e
·
verified ·
1 Parent(s): b171b5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ from fastapi import FastAPI, HTTPException, Query
4
+ from pydantic import BaseModel
5
+ from fastapi.middleware.cors import CORSMiddleware
6
+ from embeddingonnx import text_to_embedding, query_to_embedding
7
+
8
+ app = FastAPI(title="Arabic Text Embedding API")
9
+
10
+ # CORS
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ class TextRequest(BaseModel):
20
+ text: str
21
+
22
+ # REST API endpoints
23
+ @app.get("/")
24
+ def root():
25
+ return {"message": "✅ Arabic Text Embedding API is running on HF Spaces"}
26
+
27
+ @app.get("/health")
28
+ def health():
29
+ return {"status": "ok"}
30
+
31
+ @app.post("/embed")
32
+ def embed_post(req: TextRequest):
33
+ vec = text_to_embedding(req.text.strip())
34
+ return {"embedding": vec.tolist()}
35
+
36
+ @app.post("/query")
37
+ def query_post(req: TextRequest):
38
+ vec = query_to_embedding(req.text.strip())
39
+ return {"query_embedding": vec.tolist()}
40
+
41
+ # Gradio interface
42
+ def gradio_embed(text):
43
+ if not text.strip():
44
+ return "❌ النص فارغ"
45
+ vec = text_to_embedding(text.strip())
46
+ return str(vec.tolist())
47
+
48
+ demo = gr.Interface(
49
+ fn=gradio_embed,
50
+ inputs="text",
51
+ outputs="text",
52
+ title="Arabic Text Embedding",
53
+ description="قم بإدخال نص عربي للحصول على متجه embedding الخاص به."
54
+ )
55
+
56
+ # mount Gradio app على FastAPI
57
+ gr.mount_gradio_app(app, demo, path="/gradio")
58
+
59
+ # ❌ لا تحتاج uvicorn.run في Spaces