Create app_replit.py
Browse files- app_replit.py +97 -0
app_replit.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from os import environ
|
2 |
+
import logging
|
3 |
+
from fastapi import FastAPI, Request
|
4 |
+
import subprocess
|
5 |
+
# from transformers import pipeline
|
6 |
+
from huggingface_hub import InferenceClient, login
|
7 |
+
import langid
|
8 |
+
# import asyncio
|
9 |
+
|
10 |
+
# Configure logging
|
11 |
+
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s",
|
12 |
+
level=logging.INFO)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
+
# Get Hugging Face API token from environment variable
|
16 |
+
HF_HUB_TOKEN = environ.get("HUGGINGFACEHUB_API_TOKEN")
|
17 |
+
if not HF_HUB_TOKEN:
|
18 |
+
raise ValueError(
|
19 |
+
"Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN.")
|
20 |
+
|
21 |
+
# Login and initialize the client
|
22 |
+
login(token=HF_HUB_TOKEN)
|
23 |
+
client = InferenceClient(api_key=HF_HUB_TOKEN)
|
24 |
+
|
25 |
+
# Create FastAPI app
|
26 |
+
app = FastAPI()
|
27 |
+
|
28 |
+
|
29 |
+
# Function to detect language
|
30 |
+
def detect_language(user_input):
|
31 |
+
try:
|
32 |
+
lang, _ = langid.classify(user_input)
|
33 |
+
return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported"
|
34 |
+
except Exception as e:
|
35 |
+
logging.error(f"Language detection error: {e}")
|
36 |
+
return "unsupported"
|
37 |
+
|
38 |
+
|
39 |
+
# Function to generate response
|
40 |
+
def generate_response(text):
|
41 |
+
language = detect_language(text)
|
42 |
+
|
43 |
+
if language == "hebrew":
|
44 |
+
content = "转注谞讛 讘拽爪专讛 讗讘诇 转砖转祝 讗转 转讛诇讬讱 拽讘诇转 讛讛讞诇讟讜转 砖诇讱, " + text
|
45 |
+
model = "microsoft/Phi-3.5-mini-instruct"
|
46 |
+
elif language == "english":
|
47 |
+
content = "keep it short but tell your decision making process, " + text
|
48 |
+
model = "mistralai/Mistral-Nemo-Instruct-2407"
|
49 |
+
else:
|
50 |
+
return "Sorry, I only support Hebrew and English."
|
51 |
+
|
52 |
+
messages = [{"role": "user", "content": content}]
|
53 |
+
|
54 |
+
completion = client.chat.completions.create(model=model,
|
55 |
+
messages=messages,
|
56 |
+
max_tokens=2048,
|
57 |
+
temperature=0.5,
|
58 |
+
top_p=0.7)
|
59 |
+
return completion.choices[0].message.content
|
60 |
+
|
61 |
+
|
62 |
+
@app.post("/generate_response")
|
63 |
+
async def generate_text(request: Request):
|
64 |
+
"""
|
65 |
+
Endpoint to generate a response from the chat model.
|
66 |
+
Expects a JSON with a "text" field.
|
67 |
+
"""
|
68 |
+
try:
|
69 |
+
data = await request.json()
|
70 |
+
text = data.get("text", "").strip()
|
71 |
+
if not text:
|
72 |
+
return {"error": "No text provided"}
|
73 |
+
response = generate_response(text)
|
74 |
+
return {"response": response}
|
75 |
+
except Exception as e:
|
76 |
+
logging.error(f"Error processing request: {e}")
|
77 |
+
return {"error": "An unexpected error occurred."}
|
78 |
+
|
79 |
+
|
80 |
+
@app.get("/")
|
81 |
+
async def root():
|
82 |
+
"""
|
83 |
+
Root endpoint to check that the API is running.
|
84 |
+
"""
|
85 |
+
return {"message": "Decision Helper API is running!"}
|
86 |
+
|
87 |
+
|
88 |
+
# Function to run bot.py
|
89 |
+
def run_bot():
|
90 |
+
logging.info("Starting Telegram bot...")
|
91 |
+
subprocess.Popen(["python3", "bot.py"])
|
92 |
+
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
run_bot()
|
96 |
+
# import uvicorn
|
97 |
+
# uvicorn.run(app, host="0.0.0.0", port=7860)
|