Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import pipeline
|
4 |
-
import requests
|
5 |
import re
|
6 |
import os
|
7 |
from huggingface_hub import login
|
|
|
8 |
|
9 |
# Authenticate with Hugging Face
|
10 |
if "HF_TOKEN" in os.environ:
|
11 |
login(token=os.environ["HF_TOKEN"])
|
12 |
|
13 |
-
# Global
|
14 |
atlas_pipe = None
|
|
|
15 |
|
16 |
-
def
|
17 |
-
"""Load
|
18 |
-
global atlas_pipe
|
|
|
|
|
19 |
if atlas_pipe is None:
|
20 |
print("🏔️ Loading Atlas-Chat-2B model...")
|
21 |
atlas_pipe = pipeline(
|
@@ -25,7 +28,19 @@ def load_atlas_model():
|
|
25 |
device="cuda" if torch.cuda.is_available() else "cpu"
|
26 |
)
|
27 |
print("✅ Atlas-Chat model loaded!")
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
def detect_arabizi(text):
|
31 |
"""
|
@@ -84,65 +99,29 @@ def detect_arabizi(text):
|
|
84 |
|
85 |
return False
|
86 |
|
87 |
-
def
|
88 |
"""
|
89 |
-
Convert Arabizi text to Arabic using
|
90 |
"""
|
91 |
try:
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
95 |
return arabizi_text
|
96 |
|
97 |
-
|
98 |
-
|
99 |
|
100 |
-
#
|
101 |
-
|
102 |
-
"
|
103 |
-
|
104 |
-
"max_length": 512,
|
105 |
-
"num_beams": 4,
|
106 |
-
"early_stopping": True
|
107 |
-
}
|
108 |
-
}
|
109 |
|
110 |
-
|
111 |
-
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
|
112 |
|
113 |
-
# Check if request was successful
|
114 |
-
if response.status_code == 200:
|
115 |
-
result = response.json()
|
116 |
-
|
117 |
-
# Handle different response formats
|
118 |
-
if isinstance(result, list) and len(result) > 0:
|
119 |
-
if "generated_text" in result[0]:
|
120 |
-
return result[0]["generated_text"].strip()
|
121 |
-
elif isinstance(result[0], str):
|
122 |
-
return result[0].strip()
|
123 |
-
elif isinstance(result, dict) and "generated_text" in result:
|
124 |
-
return result["generated_text"].strip()
|
125 |
-
elif isinstance(result, str):
|
126 |
-
return result.strip()
|
127 |
-
else:
|
128 |
-
print(f"❌ Unexpected API response format: {result}")
|
129 |
-
return arabizi_text
|
130 |
-
|
131 |
-
elif response.status_code == 503:
|
132 |
-
print("⏳ Model is loading, falling back to original text")
|
133 |
-
return arabizi_text
|
134 |
-
else:
|
135 |
-
print(f"❌ API error {response.status_code}: {response.text}")
|
136 |
-
return arabizi_text
|
137 |
-
|
138 |
-
except requests.exceptions.Timeout:
|
139 |
-
print("⏰ API timeout, falling back to original text")
|
140 |
-
return arabizi_text
|
141 |
-
except requests.exceptions.RequestException as e:
|
142 |
-
print(f"❌ API request failed: {e}")
|
143 |
-
return arabizi_text
|
144 |
except Exception as e:
|
145 |
-
print(f"❌
|
146 |
return arabizi_text
|
147 |
|
148 |
def arabic_to_arabizi(arabic_text):
|
@@ -215,13 +194,13 @@ def arabic_to_arabizi(arabic_text):
|
|
215 |
return result.strip()
|
216 |
|
217 |
def chat_with_atlas(message, history):
|
218 |
-
"""Generate response from Atlas-Chat model with
|
219 |
if not message.strip():
|
220 |
return "ahlan wa sahlan! kifash n9der n3awnek? / مرحبا! كيفاش نقدر نعاونك؟"
|
221 |
|
222 |
try:
|
223 |
-
# Load
|
224 |
-
atlas_model =
|
225 |
|
226 |
# Detect if input is Arabizi
|
227 |
is_arabizi_input = detect_arabizi(message)
|
@@ -234,8 +213,8 @@ def chat_with_atlas(message, history):
|
|
234 |
|
235 |
# Prepare input for the model
|
236 |
if is_arabizi_input:
|
237 |
-
print("🔄 Converting Arabizi→Arabic via
|
238 |
-
arabic_input =
|
239 |
print(f"✅ ARABIC: '{arabic_input}'")
|
240 |
model_input = arabic_input
|
241 |
else:
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import pipeline
|
|
|
4 |
import re
|
5 |
import os
|
6 |
from huggingface_hub import login
|
7 |
+
from gradio_client import Client
|
8 |
|
9 |
# Authenticate with Hugging Face
|
10 |
if "HF_TOKEN" in os.environ:
|
11 |
login(token=os.environ["HF_TOKEN"])
|
12 |
|
13 |
+
# Global variables
|
14 |
atlas_pipe = None
|
15 |
+
transliteration_client = None
|
16 |
|
17 |
+
def load_models():
|
18 |
+
"""Load Atlas-Chat model and setup transliteration client"""
|
19 |
+
global atlas_pipe, transliteration_client
|
20 |
+
|
21 |
+
# Load Atlas-Chat model locally
|
22 |
if atlas_pipe is None:
|
23 |
print("🏔️ Loading Atlas-Chat-2B model...")
|
24 |
atlas_pipe = pipeline(
|
|
|
28 |
device="cuda" if torch.cuda.is_available() else "cpu"
|
29 |
)
|
30 |
print("✅ Atlas-Chat model loaded!")
|
31 |
+
|
32 |
+
# Setup transliteration client
|
33 |
+
if transliteration_client is None:
|
34 |
+
try:
|
35 |
+
# REPLACE WITH YOUR ACTUAL HELPER SPACE URL
|
36 |
+
print("🔗 Connecting to transliteration service...")
|
37 |
+
transliteration_client = Client("YOUR-USERNAME/arabizi-transliteration-helper")
|
38 |
+
print("✅ Transliteration client connected!")
|
39 |
+
except Exception as e:
|
40 |
+
print(f"❌ Failed to connect to transliteration service: {e}")
|
41 |
+
transliteration_client = None
|
42 |
+
|
43 |
+
return atlas_pipe, transliteration_client
|
44 |
|
45 |
def detect_arabizi(text):
|
46 |
"""
|
|
|
99 |
|
100 |
return False
|
101 |
|
102 |
+
def arabizi_to_arabic_client(arabizi_text):
|
103 |
"""
|
104 |
+
Convert Arabizi text to Arabic using the helper Space
|
105 |
"""
|
106 |
try:
|
107 |
+
_, client = load_models()
|
108 |
+
|
109 |
+
if client is None:
|
110 |
+
print("❌ Transliteration client not available, using fallback")
|
111 |
return arabizi_text
|
112 |
|
113 |
+
# Call the helper space
|
114 |
+
result = client.predict(arabizi_text, api_name="/predict")
|
115 |
|
116 |
+
# Check if result is an error
|
117 |
+
if isinstance(result, str) and result.startswith("Error:"):
|
118 |
+
print(f"❌ Transliteration service error: {result}")
|
119 |
+
return arabizi_text
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
return result.strip() if result else arabizi_text
|
|
|
122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
except Exception as e:
|
124 |
+
print(f"❌ Error calling transliteration service: {e}")
|
125 |
return arabizi_text
|
126 |
|
127 |
def arabic_to_arabizi(arabic_text):
|
|
|
194 |
return result.strip()
|
195 |
|
196 |
def chat_with_atlas(message, history):
|
197 |
+
"""Generate response from Atlas-Chat model with Space-to-Space Arabizi conversion"""
|
198 |
if not message.strip():
|
199 |
return "ahlan wa sahlan! kifash n9der n3awnek? / مرحبا! كيفاش نقدر نعاونك؟"
|
200 |
|
201 |
try:
|
202 |
+
# Load models
|
203 |
+
atlas_model, _ = load_models()
|
204 |
|
205 |
# Detect if input is Arabizi
|
206 |
is_arabizi_input = detect_arabizi(message)
|
|
|
213 |
|
214 |
# Prepare input for the model
|
215 |
if is_arabizi_input:
|
216 |
+
print("🔄 Converting Arabizi→Arabic via Helper Space...")
|
217 |
+
arabic_input = arabizi_to_arabic_client(message)
|
218 |
print(f"✅ ARABIC: '{arabic_input}'")
|
219 |
model_input = arabic_input
|
220 |
else:
|