File size: 2,144 Bytes
bd8871f e5d152a e12cd77 bd8871f e12cd77 e5d152a e12cd77 bd8871f e5d152a e12cd77 e5d152a 56d2e38 e12cd77 56d2e38 e12cd77 e5d152a e12cd77 e5d152a e12cd77 e5d152a e12cd77 e5d152a e12cd77 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import os
import logging
from flask import Flask, request, jsonify
from transformers import pipeline
from langdetect import detect
from huggingface_hub import login
# Initialize Flask app
app = Flask(__name__)
# Gets the Token from secrect
hf_hub_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# Logging in
login(token=hf_hub_token)
# Load Hebrew models (classification + text generation)
hebrew_classifier = pipeline("text-classification", model="onlplab/alephbert-base")
hebrew_generator = pipeline("text2text-generation", model="google/mt5-small")
# Load English models
english_classifier = pipeline("text-classification", model="mistralai/Mistral-7B-Instruct-v0.3")
english_generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
# Function to detect language
def detect_language(user_input):
try:
lang = detect(user_input)
if lang == "he":
return "hebrew"
elif lang == "en":
return "english"
else:
return "unsupported"
except:
return "unsupported"
# Function to generate response based on language
def generate_response(text):
language = detect_language(text)
if language == "hebrew":
classification = hebrew_classifier(text)
response = hebrew_generator(text, max_length=100)[0]["generated_text"]
elif language == "english":
classification = english_classifier(text)
response = english_generator(text, max_length=100)[0]["generated_text"]
else:
response = "❌ Sorry, I only support Hebrew and English."
return response
# Flask endpoint for processing text input
@app.route("/ask", methods=["POST"])
def ask():
data = request.json
user_input = data.get("text", "")
if not user_input:
return jsonify({"error": "No text provided"}), 400
response = generate_response(user_input)
return jsonify({"response": response})
# Root endpoint
@app.route("/")
def home():
return "Decision Helper Bot API is running!"
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
app.run(host="0.0.0.0", port=7860)
|