Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
-
|
|
|
|
| 5 |
import langid
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
-
logging.basicConfig(
|
| 10 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 11 |
-
level=logging.INFO
|
| 12 |
-
)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
|
| 16 |
# Get Hugging Face API token from environment variable
|
| 17 |
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 18 |
-
|
| 19 |
if not HF_HUB_TOKEN:
|
| 20 |
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN.")
|
| 21 |
|
|
@@ -29,7 +28,7 @@ client = InferenceClient(api_key=HF_HUB_TOKEN)
|
|
| 29 |
app = FastAPI()
|
| 30 |
|
| 31 |
|
| 32 |
-
def detect_language(user_input
|
| 33 |
"""
|
| 34 |
Detect the language of the input text.
|
| 35 |
Returns "hebrew" if Hebrew, "english" if English, or "unsupported" otherwise.
|
|
@@ -47,7 +46,7 @@ def detect_language(user_input: str) -> str:
|
|
| 47 |
return "unsupported"
|
| 48 |
|
| 49 |
|
| 50 |
-
def generate_response(text
|
| 51 |
"""
|
| 52 |
Generate a response based on the input text.
|
| 53 |
Selects a prompt and model according to the detected language,
|
|
@@ -57,7 +56,7 @@ def generate_response(text: str) -> str:
|
|
| 57 |
if language == "hebrew":
|
| 58 |
# Hebrew prompt: answer shortly but explain your decision-making process
|
| 59 |
content = "转砖诪讜专 注诇 转砖讜讘讛 拽爪专讛, 讗讘诇 转住驻专 讗讬讱 拽讬讘诇转 讗转 讛讛讞诇讟讛, " + text
|
| 60 |
-
model = "mistralai/Mistral-Nemo-Instruct-2407"
|
| 61 |
elif language == "english":
|
| 62 |
content = "keep it short but tell your decision making process, " + text
|
| 63 |
model = "mistralai/Mistral-Nemo-Instruct-2407"
|
|
@@ -83,8 +82,8 @@ def generate_response(text: str) -> str:
|
|
| 83 |
@app.post("/generate_response")
|
| 84 |
async def generate_text(request: Request):
|
| 85 |
"""
|
| 86 |
-
API endpoint
|
| 87 |
-
|
| 88 |
"""
|
| 89 |
try:
|
| 90 |
data = await request.json()
|
|
@@ -101,22 +100,23 @@ async def generate_text(request: Request):
|
|
| 101 |
@app.get("/")
|
| 102 |
async def root():
|
| 103 |
"""
|
| 104 |
-
Root endpoint
|
| 105 |
"""
|
| 106 |
return {"message": "Decision Helper API is running!"}
|
| 107 |
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
# When running app.py directly, start the bot as well.
|
| 119 |
-
|
| 120 |
# Uncomment the next lines to run the FastAPI server standalone.
|
|
|
|
| 121 |
import uvicorn
|
| 122 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
+
import subprocess
|
| 5 |
+
from huggingface_hub import InferenceClient, login, configure_http_backend
|
| 6 |
import langid
|
| 7 |
|
| 8 |
+
# Configure the HTTP backend to use "requests"
|
| 9 |
+
configure_http_backend("requests") # Set the HTTP backend to "requests"
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
+
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
|
| 16 |
# Get Hugging Face API token from environment variable
|
| 17 |
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
|
|
|
| 18 |
if not HF_HUB_TOKEN:
|
| 19 |
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN.")
|
| 20 |
|
|
|
|
| 28 |
app = FastAPI()
|
| 29 |
|
| 30 |
|
| 31 |
+
def detect_language(user_input):
|
| 32 |
"""
|
| 33 |
Detect the language of the input text.
|
| 34 |
Returns "hebrew" if Hebrew, "english" if English, or "unsupported" otherwise.
|
|
|
|
| 46 |
return "unsupported"
|
| 47 |
|
| 48 |
|
| 49 |
+
def generate_response(text):
|
| 50 |
"""
|
| 51 |
Generate a response based on the input text.
|
| 52 |
Selects a prompt and model according to the detected language,
|
|
|
|
| 56 |
if language == "hebrew":
|
| 57 |
# Hebrew prompt: answer shortly but explain your decision-making process
|
| 58 |
content = "转砖诪讜专 注诇 转砖讜讘讛 拽爪专讛, 讗讘诇 转住驻专 讗讬讱 拽讬讘诇转 讗转 讛讛讞诇讟讛, " + text
|
| 59 |
+
model = "mistralai/Mistral-Nemo-Instruct-2407"
|
| 60 |
elif language == "english":
|
| 61 |
content = "keep it short but tell your decision making process, " + text
|
| 62 |
model = "mistralai/Mistral-Nemo-Instruct-2407"
|
|
|
|
| 82 |
@app.post("/generate_response")
|
| 83 |
async def generate_text(request: Request):
|
| 84 |
"""
|
| 85 |
+
API endpoint to generate a response from the chat model.
|
| 86 |
+
Expects a JSON with a "text" field.
|
| 87 |
"""
|
| 88 |
try:
|
| 89 |
data = await request.json()
|
|
|
|
| 100 |
@app.get("/")
|
| 101 |
async def root():
|
| 102 |
"""
|
| 103 |
+
Root endpoint to check that the API is running.
|
| 104 |
"""
|
| 105 |
return {"message": "Decision Helper API is running!"}
|
| 106 |
|
| 107 |
|
| 108 |
+
def run_bot():
|
| 109 |
+
"""
|
| 110 |
+
Start the Telegram bot by running bot.py as a subprocess.
|
| 111 |
+
"""
|
| 112 |
+
logger.info("Starting Telegram bot...")
|
| 113 |
+
subprocess.Popen(["python3", "bot.py"])
|
| 114 |
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
# When running app.py directly, start the bot as well.
|
| 118 |
+
run_bot()
|
| 119 |
# Uncomment the next lines to run the FastAPI server standalone.
|
| 120 |
+
# Start the FastAPI server with uvicorn
|
| 121 |
import uvicorn
|
| 122 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|