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