anaspro
commited on
Commit
·
6479cb1
1
Parent(s):
eded289
updatE
Browse files
app.py
CHANGED
|
@@ -125,45 +125,47 @@ def detect_language(text):
|
|
| 125 |
arabic_ratio = arabic_chars / total_chars
|
| 126 |
return 'ar' if arabic_ratio > 0.3 else 'en'
|
| 127 |
|
|
|
|
| 128 |
@spaces.GPU()
|
| 129 |
def generate_response(input_data, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
|
| 130 |
-
#
|
| 131 |
conversation_parts = []
|
| 132 |
-
|
| 133 |
if chat_history:
|
| 134 |
for item in chat_history:
|
| 135 |
role = item["role"]
|
| 136 |
content = item["content"]
|
| 137 |
if isinstance(content, list):
|
| 138 |
content = content[0]["text"] if content and "text" in content[0] else str(content)
|
| 139 |
-
|
| 140 |
if role == "user":
|
| 141 |
conversation_parts.append(f"[|Human|] {content}")
|
| 142 |
elif role == "assistant":
|
| 143 |
conversation_parts.append(f"[|AI|] {content}")
|
| 144 |
|
| 145 |
-
# أضف الرسالة
|
| 146 |
conversation_parts.append(f"[|Human|] {input_data}")
|
| 147 |
-
|
| 148 |
-
# إنشاء البرومبت الكامل
|
| 149 |
conversation = "\n".join(conversation_parts)
|
|
|
|
|
|
|
| 150 |
full_prompt = prompt_ar.format(Question=conversation)
|
| 151 |
|
| 152 |
try:
|
| 153 |
response = get_response(full_prompt)
|
| 154 |
|
| 155 |
-
#
|
| 156 |
if "### Response :" in response:
|
| 157 |
response = response.split("### Response :")[-1].strip()
|
| 158 |
-
|
| 159 |
-
# إزالة أي محتوى إضافي أو markers
|
| 160 |
-
if "[|Human|]" in response:
|
| 161 |
-
response = response.split("[|Human|]")[0].strip()
|
| 162 |
if "[|AI|]" in response:
|
| 163 |
response = response.replace("[|AI|]", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
return response if response else "هلا، ويّاك أليكس من دعم TechSolutions، شلون أگدر أساعدك اليوم؟"
|
| 167 |
|
| 168 |
except Exception as e:
|
| 169 |
print(f"Error in generate_response: {e}")
|
|
|
|
| 125 |
arabic_ratio = arabic_chars / total_chars
|
| 126 |
return 'ar' if arabic_ratio > 0.3 else 'en'
|
| 127 |
|
| 128 |
+
@spaces.GPU()
|
| 129 |
@spaces.GPU()
|
| 130 |
def generate_response(input_data, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
|
| 131 |
+
# نبني نص المحادثة مثل ما الموديل يحب
|
| 132 |
conversation_parts = []
|
|
|
|
| 133 |
if chat_history:
|
| 134 |
for item in chat_history:
|
| 135 |
role = item["role"]
|
| 136 |
content = item["content"]
|
| 137 |
if isinstance(content, list):
|
| 138 |
content = content[0]["text"] if content and "text" in content[0] else str(content)
|
|
|
|
| 139 |
if role == "user":
|
| 140 |
conversation_parts.append(f"[|Human|] {content}")
|
| 141 |
elif role == "assistant":
|
| 142 |
conversation_parts.append(f"[|AI|] {content}")
|
| 143 |
|
| 144 |
+
# أضف الرسالة الجديدة
|
| 145 |
conversation_parts.append(f"[|Human|] {input_data}")
|
|
|
|
|
|
|
| 146 |
conversation = "\n".join(conversation_parts)
|
| 147 |
+
|
| 148 |
+
# نحطها داخل البرومبت العراقي
|
| 149 |
full_prompt = prompt_ar.format(Question=conversation)
|
| 150 |
|
| 151 |
try:
|
| 152 |
response = get_response(full_prompt)
|
| 153 |
|
| 154 |
+
# نقطع أي تعليمات أو وسوم من النتيجة
|
| 155 |
if "### Response :" in response:
|
| 156 |
response = response.split("### Response :")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
if "[|AI|]" in response:
|
| 158 |
response = response.replace("[|AI|]", "").strip()
|
| 159 |
+
if "[|Human|]" in response:
|
| 160 |
+
response = response.split("[|Human|]")[0].strip()
|
| 161 |
+
if "### Instruction:" in response:
|
| 162 |
+
response = response.split("### Instruction:")[-1].strip()
|
| 163 |
+
|
| 164 |
+
# نرجع الرد فقط
|
| 165 |
+
if not response:
|
| 166 |
+
response = "هلا، ويّاك أليكس من دعم TechSolutions، شلون أگدر أساعدك اليوم؟"
|
| 167 |
|
| 168 |
+
return response
|
|
|
|
| 169 |
|
| 170 |
except Exception as e:
|
| 171 |
print(f"Error in generate_response: {e}")
|