Update app.py
Browse files
app.py
CHANGED
|
@@ -20,24 +20,24 @@ if not HF_HUB_TOKEN:
|
|
| 20 |
login(token=HF_HUB_TOKEN)
|
| 21 |
|
| 22 |
|
| 23 |
-
# # Load Hebrew and English text generation models
|
| 24 |
-
hebrew_generator = pipeline("text-generation", model="onlplab/alephbert-base")
|
| 25 |
-
english_generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 26 |
|
| 27 |
-
# hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
|
| 28 |
-
# english_generator = pipeline("text-generation", model="distilgpt2")
|
| 29 |
|
| 30 |
|
| 31 |
-
# Function to detect language
|
| 32 |
-
def detect_language(user_input):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
|
| 43 |
# Function to generate a response
|
|
@@ -50,23 +50,41 @@ def detect_language(user_input):
|
|
| 50 |
# return "Sorry, I only support Hebrew and English."
|
| 51 |
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
def generate_response(text):
|
| 54 |
language = detect_language(text)
|
| 55 |
-
print(f"Detected language: {language}
|
| 56 |
|
| 57 |
if language == "hebrew":
|
|
|
|
| 58 |
output = hebrew_generator(text, max_length=100, truncation=True)
|
| 59 |
-
print(f"Hebrew model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 60 |
return output[0]["generated_text"]
|
| 61 |
|
| 62 |
elif language == "english":
|
|
|
|
| 63 |
output = english_generator(text, max_length=50, truncation=True)
|
| 64 |
-
print(f"English model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 65 |
return output[0]["generated_text"]
|
| 66 |
|
| 67 |
return "Sorry, I only support Hebrew and English."
|
| 68 |
|
| 69 |
|
|
|
|
| 70 |
# FastAPI lifespan event
|
| 71 |
@asynccontextmanager
|
| 72 |
async def lifespan(app: FastAPI):
|
|
|
|
| 20 |
login(token=HF_HUB_TOKEN)
|
| 21 |
|
| 22 |
|
| 23 |
+
# # # Load Hebrew and English text generation models
|
| 24 |
+
# hebrew_generator = pipeline("text-generation", model="onlplab/alephbert-base")
|
| 25 |
+
# english_generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 26 |
|
| 27 |
+
# # hebrew_generator = pipeline("text-generation", model="Norod78/hebrew-gpt_neo-small")
|
| 28 |
+
# # english_generator = pipeline("text-generation", model="distilgpt2")
|
| 29 |
|
| 30 |
|
| 31 |
+
# # Function to detect language
|
| 32 |
+
# def detect_language(user_input):
|
| 33 |
+
# try:
|
| 34 |
+
# # lang = detect(user_input)
|
| 35 |
+
# lang, _ = langid.classify(user_input) # langid.classify returns a tuple (language, confidence)
|
| 36 |
+
# print(f"Detected language: {lang}, ", f"current time: {current_time_gmt()}")
|
| 37 |
+
# return "hebrew" if lang == "he" else "english" if lang == "en" else "unsupported"
|
| 38 |
+
# except Exception as e:
|
| 39 |
+
# print(f"Language detection error: {e}")
|
| 40 |
+
# return "unsupported"
|
| 41 |
|
| 42 |
|
| 43 |
# Function to generate a response
|
|
|
|
| 50 |
# return "Sorry, I only support Hebrew and English."
|
| 51 |
|
| 52 |
|
| 53 |
+
# def generate_response(text):
|
| 54 |
+
# language = detect_language(text)
|
| 55 |
+
# print(f"Detected language: {language}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 56 |
+
|
| 57 |
+
# if language == "hebrew":
|
| 58 |
+
# output = hebrew_generator(text, max_length=100, truncation=True)
|
| 59 |
+
# print(f"Hebrew model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 60 |
+
# return output[0]["generated_text"]
|
| 61 |
+
|
| 62 |
+
# elif language == "english":
|
| 63 |
+
# output = english_generator(text, max_length=50, truncation=True)
|
| 64 |
+
# print(f"English model output: {output}, ", f"current time: {current_time_gmt()}") # Debugging
|
| 65 |
+
# return output[0]["generated_text"]
|
| 66 |
+
|
| 67 |
+
# return "Sorry, I only support Hebrew and English."
|
| 68 |
+
|
| 69 |
+
|
| 70 |
def generate_response(text):
|
| 71 |
language = detect_language(text)
|
| 72 |
+
print(f"Detected language: {language}")
|
| 73 |
|
| 74 |
if language == "hebrew":
|
| 75 |
+
hebrew_generator = pipeline("text-generation", model="onlplab/alephbert-base")
|
| 76 |
output = hebrew_generator(text, max_length=100, truncation=True)
|
|
|
|
| 77 |
return output[0]["generated_text"]
|
| 78 |
|
| 79 |
elif language == "english":
|
| 80 |
+
english_generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 81 |
output = english_generator(text, max_length=50, truncation=True)
|
|
|
|
| 82 |
return output[0]["generated_text"]
|
| 83 |
|
| 84 |
return "Sorry, I only support Hebrew and English."
|
| 85 |
|
| 86 |
|
| 87 |
+
|
| 88 |
# FastAPI lifespan event
|
| 89 |
@asynccontextmanager
|
| 90 |
async def lifespan(app: FastAPI):
|