Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,9 @@ import time
|
|
| 11 |
# Global variables
|
| 12 |
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
# Verify Hugging Face token
|
| 16 |
if not HF_HUB_TOKEN:
|
| 17 |
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
|
|
@@ -26,10 +28,9 @@ english_generator = pipeline("text-generation", model="distilgpt2")
|
|
| 26 |
# Function to detect language
|
| 27 |
def detect_language(user_input):
|
| 28 |
try:
|
| 29 |
-
current_time = time.gmtime().tm_hour+2, ":" , time.gmtime().tm_min , ":" , time.gmtime().tm_sec
|
| 30 |
# lang = detect(user_input)
|
| 31 |
lang, _ = langid.classify(user_input) # langid.classify returns a tuple (language, confidence)
|
| 32 |
-
print(f"Detected language: {lang}, ", f"current time: {
|
| 33 |
return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported"
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Language detection error: {e}")
|
|
@@ -48,17 +49,16 @@ def detect_language(user_input):
|
|
| 48 |
|
| 49 |
def generate_response(text):
|
| 50 |
language = detect_language(text)
|
| 51 |
-
|
| 52 |
-
print(f"Detected language: {language}, ", f"current time: {current_time}") # Debugging
|
| 53 |
|
| 54 |
if language == "hebrew":
|
| 55 |
output = hebrew_generator(text, max_length=100, truncation=True)
|
| 56 |
-
print(f"Hebrew model output: {output}, ", f"current time: {
|
| 57 |
return output[0]["generated_text"]
|
| 58 |
|
| 59 |
elif language == "english":
|
| 60 |
-
output = english_generator(text, max_length=
|
| 61 |
-
print(f"English model output: {output}, ", f"current time: {
|
| 62 |
return output[0]["generated_text"]
|
| 63 |
|
| 64 |
return "Sorry, I only support Hebrew and English."
|
|
|
|
| 11 |
# Global variables
|
| 12 |
HF_HUB_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 13 |
|
| 14 |
+
def current_time_gmt():
|
| 15 |
+
return time.gmtime().tm_hour+2,':',time.gmtime().tm_min,':',time.gmtime().tm_sec
|
| 16 |
+
|
| 17 |
# Verify Hugging Face token
|
| 18 |
if not HF_HUB_TOKEN:
|
| 19 |
raise ValueError("Missing Hugging Face API token. Please set HUGGINGFACEHUB_API_TOKEN in environment variables.")
|
|
|
|
| 28 |
# Function to detect language
|
| 29 |
def detect_language(user_input):
|
| 30 |
try:
|
|
|
|
| 31 |
# lang = detect(user_input)
|
| 32 |
lang, _ = langid.classify(user_input) # langid.classify returns a tuple (language, confidence)
|
| 33 |
+
print(f"Detected language: {lang}, ", f"current time: {current_time_gmt()}")
|
| 34 |
return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported"
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Language detection error: {e}")
|
|
|
|
| 49 |
|
| 50 |
def generate_response(text):
|
| 51 |
language = detect_language(text)
|
| 52 |
+
print(f"Detected language: {language}, ", f"current time: {current_time_gmt()}") # Debugging
|
|
|
|
| 53 |
|
| 54 |
if language == "hebrew":
|
| 55 |
output = hebrew_generator(text, max_length=100, truncation=True)
|
| 56 |
+
print(f"Hebrew model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 57 |
return output[0]["generated_text"]
|
| 58 |
|
| 59 |
elif language == "english":
|
| 60 |
+
output = english_generator(text, max_length=50, truncation=True)
|
| 61 |
+
print(f"English model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 62 |
return output[0]["generated_text"]
|
| 63 |
|
| 64 |
return "Sorry, I only support Hebrew and English."
|