|
|
|
import logging |
|
import os |
|
from fastapi import FastAPI, Request |
|
from contextlib import asynccontextmanager |
|
from transformers import pipeline |
|
import langid |
|
from huggingface_hub import InferenceClient, login |
|
import socket |
|
import time |
|
|
|
|
|
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN") |
|
|
|
def current_time_gmt(): |
|
return time.gmtime().tm_hour+2,':',time.gmtime().tm_min,':',time.gmtime().tm_sec |
|
|
|
|
|
if not HF_HUB_TOKEN: |
|
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.") |
|
login(token=HF_HUB_TOKEN) |
|
|
|
client = InferenceClient(api_key=HF_HUB_TOKEN) |
|
|
|
|
|
def detect_language(user_input): |
|
try: |
|
lang, _ = langid.classify(user_input) |
|
print(f"Detected language: {lang}, ", f"current time: {current_time_gmt()}") |
|
return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported" |
|
except Exception as e: |
|
print(f"Language detection error: {e}") |
|
return "unsupported" |
|
|
|
|
|
def generate_response(text): |
|
language = detect_language(text) |
|
print(f"Detected language: {language}, ", f"current time: {current_time_gmt()}") |
|
|
|
if language == "hebrew": |
|
content = "转注谞讛 讘拽爪专讛 讗讘诇 转砖转祝 讗转 转讛诇讬讱 拽讘诇转 讛讛讞诇讟讜转 砖诇讱, " + text |
|
print("content: ", content) |
|
messages = [ |
|
{ "role": "user", "content": content } |
|
] |
|
print(f"Messages: {messages}, ", f"current time: {current_time_gmt()}") |
|
|
|
completion = client.chat.completions.create( |
|
model="microsoft/Phi-3.5-mini-instruct", |
|
messages=messages, |
|
max_tokens=2048, |
|
temperature=0.5, |
|
top_p=0.7 |
|
) |
|
|
|
return completion.choices[0].message.content |
|
elif language == "english": |
|
content = "keep it short but tell your decision making process, " + text |
|
print("content: ", content) |
|
messages = [ |
|
{ "role": "user", "content": content } |
|
] |
|
print(f"Messages: {messages}, ", f"current time: {current_time_gmt()}") |
|
|
|
completion = client.chat.completions.create( |
|
model="mistralai/Mistral-Nemo-Instruct-2407", |
|
messages=messages, |
|
max_tokens=2048, |
|
temperature=0.5, |
|
top_p=0.7 |
|
) |
|
return completion.choices[0].message.content |
|
|
|
return "Sorry, I only support Hebrew and English." |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager |
|
async def lifespan(app: FastAPI): |
|
print("Starting application...") |
|
yield |
|
print("Shutting down application...") |
|
|
|
|
|
|
|
app = FastAPI(lifespan=lifespan) |
|
|
|
|
|
@app.get("/") |
|
async def root(): |
|
return {"message": "Decision Helper API is running!"} |
|
|
|
|
|
@app.post("/generate_response") |
|
async def generate_text(request: Request): |
|
try: |
|
data = await request.json() |
|
if not data or "text" not in data: |
|
logging.error("Invalid request received") |
|
return {"error": "Invalid request. Please send JSON with a 'text' field."} |
|
|
|
text = data["text"].strip() |
|
if not text: |
|
return {"error": "No text provided"} |
|
|
|
print(f"Received text: {text}") |
|
|
|
response = generate_response(text) |
|
print(f"Generated response: {response}") |
|
return {"response": response} |
|
|
|
except Exception as e: |
|
logging.error(f"Error processing request: {e}") |
|
return {"error": "An unexpected error occurred."} |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|