from llama_index.embeddings.huggingface import HuggingFaceEmbedding from flask import Flask, request,abort,jsonify from g4f.client import Client from os import getenv from requests import get as reqget from re import search app = Flask(__name__) emb = HuggingFaceEmbedding(getenv("embmodel").strip()) embcache:dict[str,list[float]] = {} chatcache:dict[str,str] = {} transcache: dict[tuple[str, str], str] = {} @app.get("/") def index(): return "Hello World!" @app.post("/api") def api(): text = request.data.decode().strip() typeofapi = request.headers.get("type") if not text or not typeofapi: abort(400,"text and type is required") if typeofapi == "embedding": if text in embcache: return embcache.get(text) result = emb.get_query_embedding(text) embcache[text] = result return jsonify(result) elif typeofapi == "chat": if text in chatcache: return chatcache.get(text) response:str = Client().chat.completions.create(max_tokens=2024,model="gpt-4o-mini",messages=[{"role": "user", "content": text}]).choices[0].message.content chatcache[text] = response return response elif typeofapi == "translate_to_en": srclang: str = request.headers.get("srclang") if not srclang: abort(400,"srclang is required") if (srclang,text) in transcache: return transcache.get((srclang,text)) if search(r"[A-Za-z]", text): # transliration origtxt = text text = reqget(f"https://inputtools.google.com/request?itc={srclang}-t-i0-und&num=1&text={text}").json()[1][0][1][0] 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]]) if origtxt: transcache[(srclang,origtxt)] = response transcache[(srclang,text)] = response return response else: abort(400,"type is invalid") @app.get("/data") def data(): return jsonify({"embcache": embcache, "chatcache": chatcache, "transcache": transcache}) app.run(host="0.0.0.0", port=7860)