Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,6 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
import re
|
| 5 |
-
import os
|
| 6 |
-
from huggingface_hub import login
|
| 7 |
-
|
| 8 |
-
# Authenticate with Hugging Face
|
| 9 |
-
if "HF_TOKEN" in os.environ:
|
| 10 |
-
login(token=os.environ["HF_TOKEN"])
|
| 11 |
|
| 12 |
# Global variables to store the models
|
| 13 |
atlas_pipe = None
|
|
@@ -225,17 +219,27 @@ def chat_with_atlas(message, history):
|
|
| 225 |
# Detect if input is Arabizi
|
| 226 |
is_arabizi_input = detect_arabizi(message)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
# Prepare input for the model
|
| 229 |
if is_arabizi_input:
|
| 230 |
# Convert Arabizi to Arabic using AI model
|
| 231 |
-
print(f"π Converting Arabizi
|
| 232 |
arabic_input = arabizi_to_arabic_ai(message)
|
| 233 |
-
print(f"β
|
| 234 |
model_input = arabic_input
|
| 235 |
else:
|
| 236 |
# Use original input (Arabic or English)
|
|
|
|
| 237 |
model_input = message
|
| 238 |
|
|
|
|
|
|
|
|
|
|
| 239 |
# Generate response using Arabic input
|
| 240 |
messages = [{"role": "user", "content": model_input}]
|
| 241 |
|
|
@@ -249,19 +253,28 @@ def chat_with_atlas(message, history):
|
|
| 249 |
|
| 250 |
# Extract the response
|
| 251 |
response = outputs[0]["generated_text"][-1]["content"].strip()
|
| 252 |
-
print(f"
|
| 253 |
|
| 254 |
# Convert response back to Arabizi if input was Arabizi
|
| 255 |
if is_arabizi_input:
|
|
|
|
| 256 |
arabizi_response = arabic_to_arabizi(response)
|
| 257 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 258 |
return arabizi_response
|
| 259 |
else:
|
| 260 |
# Return original response for Arabic/English
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
return response
|
| 262 |
|
| 263 |
except Exception as e:
|
| 264 |
-
print(f"β
|
|
|
|
| 265 |
# Return error in appropriate language
|
| 266 |
if detect_arabizi(message):
|
| 267 |
return f"sorry, kan chi mochkil: {str(e)}. 3awd jar'b!"
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Global variables to store the models
|
| 7 |
atlas_pipe = None
|
|
|
|
| 219 |
# Detect if input is Arabizi
|
| 220 |
is_arabizi_input = detect_arabizi(message)
|
| 221 |
|
| 222 |
+
print("\n" + "="*60)
|
| 223 |
+
print("π DEBUG LOG - FULL CONVERSION PIPELINE")
|
| 224 |
+
print("="*60)
|
| 225 |
+
print(f"π₯ ORIGINAL INPUT: '{message}'")
|
| 226 |
+
print(f"π€ ARABIZI DETECTED: {is_arabizi_input}")
|
| 227 |
+
|
| 228 |
# Prepare input for the model
|
| 229 |
if is_arabizi_input:
|
| 230 |
# Convert Arabizi to Arabic using AI model
|
| 231 |
+
print(f"\nπ STEP 1: Converting Arabizi to Arabic...")
|
| 232 |
arabic_input = arabizi_to_arabic_ai(message)
|
| 233 |
+
print(f"β
ARABIC CONVERSION: '{arabic_input}'")
|
| 234 |
model_input = arabic_input
|
| 235 |
else:
|
| 236 |
# Use original input (Arabic or English)
|
| 237 |
+
print(f"\nβ‘οΈ NO CONVERSION NEEDED - Using original input")
|
| 238 |
model_input = message
|
| 239 |
|
| 240 |
+
print(f"\nπ€ STEP 2: Sending to Atlas-Chat model...")
|
| 241 |
+
print(f"π€ MODEL INPUT: '{model_input}'")
|
| 242 |
+
|
| 243 |
# Generate response using Arabic input
|
| 244 |
messages = [{"role": "user", "content": model_input}]
|
| 245 |
|
|
|
|
| 253 |
|
| 254 |
# Extract the response
|
| 255 |
response = outputs[0]["generated_text"][-1]["content"].strip()
|
| 256 |
+
print(f"β
MODEL RESPONSE (Arabic): '{response}'")
|
| 257 |
|
| 258 |
# Convert response back to Arabizi if input was Arabizi
|
| 259 |
if is_arabizi_input:
|
| 260 |
+
print(f"\nπ STEP 3: Converting response back to Arabizi...")
|
| 261 |
arabizi_response = arabic_to_arabizi(response)
|
| 262 |
+
print(f"β
FINAL ARABIZI RESPONSE: '{arabizi_response}'")
|
| 263 |
+
print("="*60)
|
| 264 |
+
print("π― FINAL OUTPUT TO USER:", arabizi_response)
|
| 265 |
+
print("="*60 + "\n")
|
| 266 |
return arabizi_response
|
| 267 |
else:
|
| 268 |
# Return original response for Arabic/English
|
| 269 |
+
print(f"\nβ‘οΈ NO BACK-CONVERSION NEEDED")
|
| 270 |
+
print("="*60)
|
| 271 |
+
print("π― FINAL OUTPUT TO USER:", response)
|
| 272 |
+
print("="*60 + "\n")
|
| 273 |
return response
|
| 274 |
|
| 275 |
except Exception as e:
|
| 276 |
+
print(f"\nβ ERROR OCCURRED: {str(e)}")
|
| 277 |
+
print("="*60 + "\n")
|
| 278 |
# Return error in appropriate language
|
| 279 |
if detect_arabizi(message):
|
| 280 |
return f"sorry, kan chi mochkil: {str(e)}. 3awd jar'b!"
|