Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,26 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, Request
|
| 3 |
-
import uvicorn
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
app
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
print(body);
|
| 28 |
-
|
| 29 |
-
model = body['model']
|
| 30 |
-
text = body['input'];
|
| 31 |
-
embeddings = embed(text)
|
| 32 |
-
return {
|
| 33 |
-
'object': "list"
|
| 34 |
-
,'data': [{
|
| 35 |
-
'object': "embeddings"
|
| 36 |
-
,'embedding': embeddings
|
| 37 |
-
,'index':0
|
| 38 |
-
}]
|
| 39 |
-
,'model':model
|
| 40 |
-
,'usage':{
|
| 41 |
-
'prompt_tokens': 0
|
| 42 |
-
,'total_tokens': 0
|
| 43 |
-
}
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
def fn(text):
|
| 47 |
-
return embed(text);
|
| 48 |
-
|
| 49 |
-
with gr.Blocks(fill_height=True) as demo:
|
| 50 |
-
text = gr.Textbox();
|
| 51 |
-
embeddings = gr.Textbox()
|
| 52 |
-
|
| 53 |
-
text.submit(fn, [text], [embeddings]);
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print("Loading embedding model");
|
| 57 |
-
Embedder = None #SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1")
|
| 58 |
-
|
| 59 |
-
# demo.run_startup_events()
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
#demo.launch(
|
| 63 |
-
# share=False,
|
| 64 |
-
# debug=False,
|
| 65 |
-
# server_port=7860,
|
| 66 |
-
# server_name="0.0.0.0",
|
| 67 |
-
# allowed_paths=[]
|
| 68 |
-
#)
|
| 69 |
-
|
| 70 |
-
print("Demo run...");
|
| 71 |
-
(app2,url,other) = demo.launch(prevent_thread_lock=True, server_name=None, server_port=8000);
|
| 72 |
-
|
| 73 |
-
print("Mounting app...");
|
| 74 |
-
GradioApp = gr.mount_gradio_app(app, demo, path="/", ssr_mode=False);
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
demo.close();
|
| 78 |
-
|
| 79 |
-
if __name__ == '__main__':
|
| 80 |
-
print("Running uviconr...");
|
| 81 |
-
uvicorn.run(GradioApp, host="0.0.0.0", port=7860)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
import uvicorn
|
| 4 |
+
import spaces
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@spaces.GPU
|
| 9 |
+
def embed(text):
|
| 10 |
+
# my embedding logic here (e.g: sentence transformers)
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
@app.post("/v1/embeddings")
|
| 14 |
+
def openai_embed(req:Request):
|
| 15 |
+
# some logic that will call embed
|
| 16 |
+
embed("some data from request")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
with gr.Blocks() as demo:
|
| 20 |
+
text = gr.Textbox();
|
| 21 |
+
embeddings = gr.Textbox()
|
| 22 |
+
|
| 23 |
+
text.submit(embed, [text], [embeddings]);
|
| 24 |
+
|
| 25 |
+
GradioApp = gr.mount_gradio_app(app, demo, path="/");
|
| 26 |
+
uvicorn.run(GradioApp, port=7860, host="0.0.0.0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|