Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,6 @@ from typing import Iterator, List, Tuple
|
|
6 |
|
7 |
# Configuration Constants
|
8 |
DEFAULT_SYSTEM_PROMPT = """
|
9 |
-
|
10 |
|
11 |
أنت مترجم ثنائي اللغة متخصص في الترجمة بين العربية والإنجليزية. هدفك هو تقديم ترجمات دقيقة، ملائمة للسياق، ومتسقة من الناحية الأسلوبية، مع الالتزام بالإرشادات التالية:
|
12 |
أسلوب الكتابة:
|
@@ -42,90 +41,53 @@ DEFAULT_SYSTEM_PROMPT = """
|
|
42 |
قدم الأولوية للوضوح، والتناسق، والملاءمة مع احتياجات الجمهور المستهدف. قم دائمًا بموازنة التعليمات الخاصة بالمشروع مع هذه الإرشادات، مع إعطاء الأولوية لمتطلبات العميل عند وجود أي تعارض.
|
43 |
|
44 |
|
45 |
-
|
|
|
46 |
|
47 |
TITLE = "<h1><center>Mawared T Assistant</center></h1>"
|
48 |
PLACEHOLDER = "Ask me anything! I'll think through it step by step."
|
49 |
|
50 |
CSS = """
|
51 |
-
:root {
|
52 |
-
--rtl-bg: #f0f8ff;
|
53 |
-
--ltr-bg: #fff8f0;
|
54 |
-
}
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
}
|
61 |
-
|
62 |
-
|
63 |
-
direction: ltr !important;
|
64 |
-
text-align: left !important;
|
65 |
}
|
66 |
-
|
67 |
.message-wrap {
|
68 |
overflow-x: auto;
|
69 |
-
padding: 20px;
|
70 |
-
border-radius: 10px;
|
71 |
-
margin: 10px 0;
|
72 |
-
}
|
73 |
-
|
74 |
-
.user-message {
|
75 |
-
background: var(--rtl-bg);
|
76 |
-
padding: 15px;
|
77 |
-
border-radius: 15px 15px 0 15px;
|
78 |
-
margin: 10px 0;
|
79 |
-
max-width: 80%;
|
80 |
-
margin-left: auto;
|
81 |
}
|
82 |
-
|
83 |
-
|
84 |
-
background: var(--ltr-bg);
|
85 |
-
padding: 15px;
|
86 |
-
border-radius: 15px 15px 15px 0;
|
87 |
-
margin: 10px 0;
|
88 |
-
max-width: 80%;
|
89 |
-
}
|
90 |
-
|
91 |
-
.direction-toggle {
|
92 |
-
position: absolute;
|
93 |
-
right: 10px;
|
94 |
-
top: 10px;
|
95 |
-
z-index: 1000;
|
96 |
}
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
101 |
}
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
105 |
}
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
direction: rtl !important;
|
110 |
}
|
111 |
-
|
112 |
.chat-area {
|
113 |
height: 500px !important;
|
114 |
overflow-y: auto !important;
|
115 |
-
background: #f8f9fa !important;
|
116 |
-
border-radius: 10px !important;
|
117 |
-
padding: 20px !important;
|
118 |
}
|
119 |
|
120 |
-
.duplicate-button {
|
121 |
-
margin: auto !important;
|
122 |
-
color: white !important;
|
123 |
-
background: black !important;
|
124 |
-
border-radius: 100vh !important;
|
125 |
-
}
|
126 |
"""
|
127 |
|
128 |
-
# Precompile regex patterns
|
129 |
TAG_PATTERNS = [
|
130 |
(re.compile(r'<Thinking>'), '\n<Thinking>\n'),
|
131 |
(re.compile(r'</Thinking>'), '\n</Thinking>\n'),
|
@@ -137,12 +99,6 @@ TAG_PATTERNS = [
|
|
137 |
(re.compile(r'</Final>'), '\n</Final>\n')
|
138 |
]
|
139 |
|
140 |
-
ARABIC_REGEX = re.compile(r'[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]')
|
141 |
-
|
142 |
-
def has_arabic(text: str) -> bool:
|
143 |
-
"""Check if text contains Arabic characters"""
|
144 |
-
return bool(ARABIC_REGEX.search(text))
|
145 |
-
|
146 |
def format_text(text: str) -> str:
|
147 |
"""Format text with proper spacing and tag highlighting"""
|
148 |
formatted = text
|
@@ -151,28 +107,11 @@ def format_text(text: str) -> str:
|
|
151 |
return '\n'.join(line for line in formatted.split('\n') if line.strip())
|
152 |
|
153 |
def format_chat_history(history: List[List[str]]) -> str:
|
154 |
-
"""Format chat history
|
155 |
-
|
156 |
-
|
157 |
-
if user_msg
|
158 |
-
|
159 |
-
html.append(f"""
|
160 |
-
<div class="message-wrap {direction}">
|
161 |
-
<div class="user-message arabic-text {direction}">
|
162 |
-
<strong>User:</strong> {user_msg}
|
163 |
-
</div>
|
164 |
-
</div>
|
165 |
-
""")
|
166 |
-
if assistant_msg:
|
167 |
-
direction = "rtl" if has_arabic(assistant_msg) else "ltr"
|
168 |
-
html.append(f"""
|
169 |
-
<div class="message-wrap {direction}">
|
170 |
-
<div class="assistant-message {direction}">
|
171 |
-
<strong>Assistant:</strong> {assistant_msg}
|
172 |
-
</div>
|
173 |
-
</div>
|
174 |
-
""")
|
175 |
-
return "".join(html)
|
176 |
|
177 |
def convert_history_to_cohere_format(history: List[List[str]]) -> List[dict]:
|
178 |
"""Convert chat history to Cohere's format with proper roles"""
|
@@ -189,14 +128,14 @@ def chat_response(
|
|
189 |
history: List[List[str]],
|
190 |
chat_display: str,
|
191 |
system_prompt: str,
|
192 |
-
temperature: float = 0.
|
193 |
max_new_tokens: int = 8192,
|
194 |
top_p: float = 0.8,
|
195 |
top_k: int = 40,
|
196 |
penalty: float = 1.2,
|
197 |
api_key: str = os.getenv("COHERE_API_KEY")
|
198 |
) -> Iterator[Tuple[List[List[str]], str]]:
|
199 |
-
"""Generate chat responses using Cohere API"""
|
200 |
if not api_key:
|
201 |
error_message = "Error: COHERE_API_KEY environment variable not set."
|
202 |
history.append([message, error_message])
|
@@ -245,9 +184,10 @@ def main():
|
|
245 |
with gr.Row():
|
246 |
with gr.Column():
|
247 |
chat_history = gr.State([])
|
248 |
-
chat_display = gr.
|
249 |
value="",
|
250 |
label="Chat History",
|
|
|
251 |
elem_classes=["chat-area"],
|
252 |
)
|
253 |
|
@@ -255,8 +195,7 @@ def main():
|
|
255 |
placeholder=PLACEHOLDER,
|
256 |
label="Your message",
|
257 |
lines=3,
|
258 |
-
elem_id="message_input"
|
259 |
-
elem_classes=["text-input"]
|
260 |
)
|
261 |
|
262 |
with gr.Row():
|
@@ -268,13 +207,12 @@ def main():
|
|
268 |
value=DEFAULT_SYSTEM_PROMPT,
|
269 |
label="System Prompt",
|
270 |
lines=5,
|
271 |
-
elem_classes=["rtl-textarea"]
|
272 |
)
|
273 |
temperature = gr.Slider(
|
274 |
minimum=0,
|
275 |
maximum=1,
|
276 |
step=0.1,
|
277 |
-
value=0.
|
278 |
label="Temperature",
|
279 |
)
|
280 |
max_tokens = gr.Slider(
|
@@ -329,8 +267,28 @@ def main():
|
|
329 |
show_progress=True,
|
330 |
)
|
331 |
|
332 |
-
|
333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
|
335 |
if __name__ == "__main__":
|
336 |
demo = main()
|
|
|
6 |
|
7 |
# Configuration Constants
|
8 |
DEFAULT_SYSTEM_PROMPT = """
|
|
|
9 |
|
10 |
أنت مترجم ثنائي اللغة متخصص في الترجمة بين العربية والإنجليزية. هدفك هو تقديم ترجمات دقيقة، ملائمة للسياق، ومتسقة من الناحية الأسلوبية، مع الالتزام بالإرشادات التالية:
|
11 |
أسلوب الكتابة:
|
|
|
41 |
قدم الأولوية للوضوح، والتناسق، والملاءمة مع احتياجات الجمهور المستهدف. قم دائمًا بموازنة التعليمات الخاصة بالمشروع مع هذه الإرشادات، مع إعطاء الأولوية لمتطلبات العميل عند وجود أي تعارض.
|
42 |
|
43 |
|
44 |
+
|
45 |
+
""" # Full prompt maintained as original
|
46 |
|
47 |
TITLE = "<h1><center>Mawared T Assistant</center></h1>"
|
48 |
PLACEHOLDER = "Ask me anything! I'll think through it step by step."
|
49 |
|
50 |
CSS = """
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
duplicate-button {
|
53 |
+
margin: auto !important;
|
54 |
+
color: white !important;
|
55 |
+
background: black !important;
|
56 |
+
border-radius: 100vh !important;
|
57 |
}
|
58 |
+
h3 {
|
59 |
+
text-align: center;
|
|
|
|
|
60 |
}
|
|
|
61 |
.message-wrap {
|
62 |
overflow-x: auto;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
}
|
64 |
+
.message-wrap p {
|
65 |
+
margin-bottom: 1em;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
}
|
67 |
+
.message-wrap pre {
|
68 |
+
background-color: #f6f8fa;
|
69 |
+
border-radius: 3px;
|
70 |
+
padding: 16px;
|
71 |
+
overflow-x: auto;
|
72 |
}
|
73 |
+
.message-wrap code {
|
74 |
+
background-color: rgba(175,184,193,0.2);
|
75 |
+
border-radius: 3px;
|
76 |
+
padding: 0.2em 0.4em;
|
77 |
+
font-family: monospace;
|
78 |
}
|
79 |
+
.custom-tag {
|
80 |
+
color: #0066cc;
|
81 |
+
font-weight: bold;
|
|
|
82 |
}
|
|
|
83 |
.chat-area {
|
84 |
height: 500px !important;
|
85 |
overflow-y: auto !important;
|
|
|
|
|
|
|
86 |
}
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
"""
|
89 |
|
90 |
+
# Precompile regex patterns for better performance
|
91 |
TAG_PATTERNS = [
|
92 |
(re.compile(r'<Thinking>'), '\n<Thinking>\n'),
|
93 |
(re.compile(r'</Thinking>'), '\n</Thinking>\n'),
|
|
|
99 |
(re.compile(r'</Final>'), '\n</Final>\n')
|
100 |
]
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
def format_text(text: str) -> str:
|
103 |
"""Format text with proper spacing and tag highlighting"""
|
104 |
formatted = text
|
|
|
107 |
return '\n'.join(line for line in formatted.split('\n') if line.strip())
|
108 |
|
109 |
def format_chat_history(history: List[List[str]]) -> str:
|
110 |
+
"""Format chat history for display"""
|
111 |
+
return "\n\n".join(
|
112 |
+
f"User: {user_msg}\nAssistant: {assistant_msg}"
|
113 |
+
for user_msg, assistant_msg in history if user_msg
|
114 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
def convert_history_to_cohere_format(history: List[List[str]]) -> List[dict]:
|
117 |
"""Convert chat history to Cohere's format with proper roles"""
|
|
|
128 |
history: List[List[str]],
|
129 |
chat_display: str,
|
130 |
system_prompt: str,
|
131 |
+
temperature: float = 0.2,
|
132 |
max_new_tokens: int = 8192,
|
133 |
top_p: float = 0.8,
|
134 |
top_k: int = 40,
|
135 |
penalty: float = 1.2,
|
136 |
api_key: str = os.getenv("COHERE_API_KEY")
|
137 |
) -> Iterator[Tuple[List[List[str]], str]]:
|
138 |
+
"""Generate chat responses using Cohere API with error handling"""
|
139 |
if not api_key:
|
140 |
error_message = "Error: COHERE_API_KEY environment variable not set."
|
141 |
history.append([message, error_message])
|
|
|
184 |
with gr.Row():
|
185 |
with gr.Column():
|
186 |
chat_history = gr.State([])
|
187 |
+
chat_display = gr.TextArea(
|
188 |
value="",
|
189 |
label="Chat History",
|
190 |
+
interactive=False,
|
191 |
elem_classes=["chat-area"],
|
192 |
)
|
193 |
|
|
|
195 |
placeholder=PLACEHOLDER,
|
196 |
label="Your message",
|
197 |
lines=3,
|
198 |
+
elem_id="message_input"
|
|
|
199 |
)
|
200 |
|
201 |
with gr.Row():
|
|
|
207 |
value=DEFAULT_SYSTEM_PROMPT,
|
208 |
label="System Prompt",
|
209 |
lines=5,
|
|
|
210 |
)
|
211 |
temperature = gr.Slider(
|
212 |
minimum=0,
|
213 |
maximum=1,
|
214 |
step=0.1,
|
215 |
+
value=0.2,
|
216 |
label="Temperature",
|
217 |
)
|
218 |
max_tokens = gr.Slider(
|
|
|
267 |
show_progress=True,
|
268 |
)
|
269 |
|
270 |
+
# Add JavaScript for Enter key handling
|
271 |
+
demo.load(
|
272 |
+
None,
|
273 |
+
None,
|
274 |
+
None,
|
275 |
+
_js="""
|
276 |
+
function enableEnterSubmit() {
|
277 |
+
const textarea = document.querySelector('#message_input textarea');
|
278 |
+
const submitBtn = document.querySelector('#submit_btn');
|
279 |
+
|
280 |
+
textarea?.addEventListener('keydown', (e) => {
|
281 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
282 |
+
e.preventDefault();
|
283 |
+
submitBtn.click();
|
284 |
+
}
|
285 |
+
});
|
286 |
+
}
|
287 |
+
enableEnterSubmit();
|
288 |
+
"""
|
289 |
+
)
|
290 |
+
|
291 |
+
return demo
|
292 |
|
293 |
if __name__ == "__main__":
|
294 |
demo = main()
|