Spaces:
Running
Running
shethjenil
commited on
Commit
•
96b4814
1
Parent(s):
8fb512f
Upload 2 files
Browse files- Dockerfile +8 -0
- app.py +53 -0
Dockerfile
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.11-slim
|
2 |
+
RUN useradd -ms /bin/bash myuser
|
3 |
+
WORKDIR /code
|
4 |
+
COPY . .
|
5 |
+
RUN pip install flask llama-index-embeddings-huggingface g4f[all]
|
6 |
+
RUN chown -R myuser:myuser /code
|
7 |
+
USER myuser
|
8 |
+
CMD ["python","app.py"]
|
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
2 |
+
from flask import Flask, request,abort,jsonify
|
3 |
+
from g4f.client import Client
|
4 |
+
from os import getenv
|
5 |
+
from requests import get as reqget
|
6 |
+
from re import search
|
7 |
+
app = Flask(__name__)
|
8 |
+
emb = HuggingFaceEmbedding(getenv("embmodel").strip())
|
9 |
+
embcache:dict[str,list[float]] = {}
|
10 |
+
chatcache:dict[str,str] = {}
|
11 |
+
transcache: dict[tuple[str, str], str] = {}
|
12 |
+
@app.get("/")
|
13 |
+
def index():
|
14 |
+
return "Hello World!"
|
15 |
+
@app.post("/api")
|
16 |
+
def api():
|
17 |
+
text = request.data.decode().strip()
|
18 |
+
typeofapi = request.headers.get("type")
|
19 |
+
if not text or not typeofapi:
|
20 |
+
abort(400,"text and type is required")
|
21 |
+
if typeofapi == "embedding":
|
22 |
+
if text in embcache:
|
23 |
+
return embcache.get(text)
|
24 |
+
result = emb.get_query_embedding(text)
|
25 |
+
embcache[text] = result
|
26 |
+
return jsonify(result)
|
27 |
+
elif typeofapi == "chat":
|
28 |
+
if text in chatcache:
|
29 |
+
return chatcache.get(text)
|
30 |
+
response:str = Client().chat.completions.create(max_tokens=2024,model="gpt-4o-mini",messages=[{"role": "user", "content": text}]).choices[0].message.content
|
31 |
+
chatcache[text] = response
|
32 |
+
return response
|
33 |
+
elif typeofapi == "translate_to_en":
|
34 |
+
srclang: str = request.headers.get("srclang")
|
35 |
+
if not srclang:
|
36 |
+
abort(400,"srclang is required")
|
37 |
+
if (srclang,text) in transcache:
|
38 |
+
return transcache.get((srclang,text))
|
39 |
+
if search(r"[A-Za-z]", text): # transliration
|
40 |
+
origtxt = text
|
41 |
+
text = reqget(f"https://inputtools.google.com/request?itc={srclang}-t-i0-und&num=1&text={text}").json()[1][0][1][0]
|
42 |
+
response:str = "".join([i[0] for i in reqget(f'https://translate.googleapis.com/translate_a/single?client=gtx&sl={srclang}&tl=en&dt=t&q={text}').json()[0]])
|
43 |
+
if origtxt:
|
44 |
+
transcache[(srclang,origtxt)] = response
|
45 |
+
transcache[(srclang,text)] = response
|
46 |
+
return response
|
47 |
+
else:
|
48 |
+
abort(400,"type is invalid")
|
49 |
+
@app.get("/data")
|
50 |
+
def data():
|
51 |
+
return jsonify({"embcache": embcache, "chatcache": chatcache, "transcache": transcache})
|
52 |
+
|
53 |
+
app.run(host="0.0.0.0", port=7860)
|