Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,48 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# [1] إعداد الاتصال ال
|
| 7 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 8 |
API_URL = "https://api-inference.huggingface.co/models/aab20abdullah/Akin-Yurt-Llama3-8B"
|
| 9 |
|
| 10 |
-
# [2] مي
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
UKÜCÜ_YEMINI = """ALLAHA, KURANA, YEMİN OLSUN.
|
| 12 |
ŞEHİTLERİM, GAZİLERİM, EMİN OLSUN.
|
| 13 |
ÜLKÜCÜ TÜRK GENÇLİĞİ OLARAK:
|
|
@@ -19,30 +55,32 @@ YILANLARDAN OLMAYACAĞIZ! SATANLARDAN OLMAYACAĞIZ! KAÇANLARDAN OLMAYACAĞIZ!
|
|
| 19 |
YIKILMAYACAĞIZ. BAŞARACAĞIZ, BAŞARACAĞIZ, BAŞARACAĞIZ.
|
| 20 |
TANRI TÜRKÜ KORUSUN VE YÜCELTSİN. (AMİN)"""
|
| 21 |
|
| 22 |
-
|
| 23 |
Geliştiricilerin: Abdullah Ali Bahaaldeen، Ali El-Hürmüzlü ve Usame Alaaddin. TÖGB birimi tarafından geliştirildin."""
|
| 24 |
|
| 25 |
-
# [
|
| 26 |
def respond(message, history):
|
| 27 |
-
#
|
| 28 |
yemin_triggers = ["yemin", "yemin et", "andı oku", "yemini oku"]
|
| 29 |
if any(trigger in message.lower() for trigger in yemin_triggers):
|
| 30 |
yield UKÜCÜ_YEMINI
|
| 31 |
return
|
| 32 |
|
| 33 |
-
# فحص التوكن
|
| 34 |
if not HF_TOKEN:
|
| 35 |
yield "⚠️ HATA: HF_TOKEN bulunamadı. Lütfen Space ayarlarından ekleyin."
|
| 36 |
return
|
| 37 |
|
| 38 |
-
# ب
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
for user_msg, assistant_msg in history:
|
| 41 |
if user_msg: prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|>"
|
| 42 |
if assistant_msg: prompt += f"<|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
|
| 43 |
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 44 |
|
| 45 |
-
# تجهيز الطلب المباشر للخادم
|
| 46 |
headers = {
|
| 47 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 48 |
"Content-Type": "application/json"
|
|
@@ -55,23 +93,21 @@ def respond(message, history):
|
|
| 55 |
"top_p": 0.95,
|
| 56 |
"return_full_text": False
|
| 57 |
},
|
| 58 |
-
"stream": True
|
| 59 |
}
|
| 60 |
|
|
|
|
| 61 |
try:
|
| 62 |
-
# إرسال الطلب
|
| 63 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 64 |
|
| 65 |
-
# معالجة أخطاء الخادم (مثل: النموذج قيد التحميل)
|
| 66 |
if response.status_code != 200:
|
| 67 |
error_data = response.json()
|
| 68 |
if "currently loading" in str(error_data):
|
| 69 |
-
yield f"⏳ Akın Yurt
|
| 70 |
else:
|
| 71 |
yield f"⚠️ Sunucu Hatası ({response.status_code}): {error_data.get('error', str(error_data))}"
|
| 72 |
return
|
| 73 |
|
| 74 |
-
# قراءة البث المباشر (Streaming)
|
| 75 |
output_text = ""
|
| 76 |
for line in response.iter_lines():
|
| 77 |
if line:
|
|
@@ -84,30 +120,30 @@ def respond(message, history):
|
|
| 84 |
data_json = json.loads(data_str)
|
| 85 |
token = data_json.get("token", {}).get("text", "")
|
| 86 |
output_text += token
|
| 87 |
-
# إرسال الكلمات للواجهة وتجاهل وسوم Llama
|
| 88 |
yield output_text.replace("<|eot_id|>", "").strip()
|
| 89 |
except:
|
| 90 |
continue
|
| 91 |
except Exception as e:
|
| 92 |
yield f"⚠️ API Bağlantı Çöktü: {str(e)}"
|
| 93 |
|
| 94 |
-
# [
|
| 95 |
-
custom_theme = gr.themes.Soft(
|
| 96 |
-
primary_hue="blue",
|
| 97 |
-
secondary_hue="cyan",
|
| 98 |
-
).set(
|
| 99 |
button_primary_background_fill="#007bff",
|
| 100 |
button_primary_text_color="white",
|
| 101 |
)
|
| 102 |
|
| 103 |
with gr.Blocks(theme=custom_theme, title="Akın Yurt - TÖGB") as demo:
|
| 104 |
gr.Markdown("# 🐺 Akın Yurt")
|
| 105 |
-
gr.Markdown("### Türkmeneli Dijital Asistanı")
|
| 106 |
gr.Markdown("---")
|
| 107 |
|
| 108 |
gr.ChatInterface(
|
| 109 |
respond,
|
| 110 |
-
examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
cache_examples=False,
|
| 112 |
)
|
| 113 |
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
|
| 8 |
+
# [1] إعدادات الاتصال والبيئة
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
API_URL = "https://api-inference.huggingface.co/models/aab20abdullah/Akin-Yurt-Llama3-8B"
|
| 11 |
|
| 12 |
+
# [2] تحميل قاعدة بيانات الشهداء (الذاكرة الوطنية)
|
| 13 |
+
print("📥 Şehitler Veritabanı Yükleniyor...")
|
| 14 |
+
try:
|
| 15 |
+
# نقوم بتحميل البيانات باستخدام التوكن للوصول للمستودعات الخاصة
|
| 16 |
+
dataset = load_dataset("aab20abdullah/turkmen-martyrs-dataset", split="train", token=HF_TOKEN)
|
| 17 |
+
martyrs_df = dataset.to_pandas()
|
| 18 |
+
print(f"✅ Başarılı: {len(martyrs_df)} kayıt yüklendi.")
|
| 19 |
+
except Exception as e:
|
| 20 |
+
martyrs_df = pd.DataFrame()
|
| 21 |
+
print(f"⚠️ Veritabanı Yükleme Hatası: {e}")
|
| 22 |
+
|
| 23 |
+
# دالة البحث السريع في قاعدة البيانات
|
| 24 |
+
def search_martyrs(query):
|
| 25 |
+
if martyrs_df.empty:
|
| 26 |
+
return ""
|
| 27 |
+
|
| 28 |
+
query_words = query.lower().split()
|
| 29 |
+
results = []
|
| 30 |
+
|
| 31 |
+
# بحث بسيط يطابق كلمات السؤال مع سجلات قاعدة البيانات
|
| 32 |
+
for _, row in martyrs_df.iterrows():
|
| 33 |
+
# دمج كل محتوى الصف في نص واحد للبحث
|
| 34 |
+
row_text = " | ".join([str(val) for val in row.values])
|
| 35 |
+
# إذا تطابقت أي كلمة مهمة من السؤال مع السجل (أطول من 3 حروف لتجنب الحروف البسيطة)
|
| 36 |
+
if any(word in row_text.lower() for word in query_words if len(word) > 3):
|
| 37 |
+
results.append(row_text)
|
| 38 |
+
# نكتفي بأهم 3 نتائج لعدم إرباك ذاكرة النموذج
|
| 39 |
+
if len(results) >= 3:
|
| 40 |
+
break
|
| 41 |
+
|
| 42 |
+
if results:
|
| 43 |
+
return "\n\n[RESMİ VERİTABANI BİLGİSİ - BU BİLGİYİ KULLANARAK CEVAP VER]:\n" + "\n".join(results)
|
| 44 |
+
return ""
|
| 45 |
+
|
| 46 |
+
# [3] ميثاق القسم التركماني والهوية
|
| 47 |
UKÜCÜ_YEMINI = """ALLAHA, KURANA, YEMİN OLSUN.
|
| 48 |
ŞEHİTLERİM, GAZİLERİM, EMİN OLSUN.
|
| 49 |
ÜLKÜCÜ TÜRK GENÇLİĞİ OLARAK:
|
|
|
|
| 55 |
YIKILMAYACAĞIZ. BAŞARACAĞIZ, BAŞARACAĞIZ, BAŞARACAĞIZ.
|
| 56 |
TANRI TÜRKÜ KORUSUN VE YÜCELTSİN. (AMİN)"""
|
| 57 |
|
| 58 |
+
BASE_SYSTEM_PROMPT = """Senin adın Akın Yurt. Kerkük'te görev yapan، Türkmeneli davasına adanmış، zeki bir asistansın.
|
| 59 |
Geliştiricilerin: Abdullah Ali Bahaaldeen، Ali El-Hürmüzlü ve Usame Alaaddin. TÖGB birimi tarafından geliştirildin."""
|
| 60 |
|
| 61 |
+
# [4] المعالجة المركزية
|
| 62 |
def respond(message, history):
|
| 63 |
+
# 1. الاستجابة الفورية للقسم
|
| 64 |
yemin_triggers = ["yemin", "yemin et", "andı oku", "yemini oku"]
|
| 65 |
if any(trigger in message.lower() for trigger in yemin_triggers):
|
| 66 |
yield UKÜCÜ_YEMINI
|
| 67 |
return
|
| 68 |
|
|
|
|
| 69 |
if not HF_TOKEN:
|
| 70 |
yield "⚠️ HATA: HF_TOKEN bulunamadı. Lütfen Space ayarlarından ekleyin."
|
| 71 |
return
|
| 72 |
|
| 73 |
+
# 2. البحث في قاعدة بيانات الشهداء ودمجها مع شخصية النموذج
|
| 74 |
+
db_context = search_martyrs(message)
|
| 75 |
+
dynamic_system_prompt = BASE_SYSTEM_PROMPT + db_context
|
| 76 |
+
|
| 77 |
+
# 3. بناء هيكل المحادثة (Prompt Engineering)
|
| 78 |
+
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{dynamic_system_prompt}<|eot_id|>"
|
| 79 |
for user_msg, assistant_msg in history:
|
| 80 |
if user_msg: prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|>"
|
| 81 |
if assistant_msg: prompt += f"<|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
|
| 82 |
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 83 |
|
|
|
|
| 84 |
headers = {
|
| 85 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 86 |
"Content-Type": "application/json"
|
|
|
|
| 93 |
"top_p": 0.95,
|
| 94 |
"return_full_text": False
|
| 95 |
},
|
| 96 |
+
"stream": True
|
| 97 |
}
|
| 98 |
|
| 99 |
+
# 4. الاتصال المباشر وتوليد الرد
|
| 100 |
try:
|
|
|
|
| 101 |
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 102 |
|
|
|
|
| 103 |
if response.status_code != 200:
|
| 104 |
error_data = response.json()
|
| 105 |
if "currently loading" in str(error_data):
|
| 106 |
+
yield f"⏳ Akın Yurt uyanıyor... Lütfen {error_data.get('estimated_time', 20)} saniye bekleyip tekrar yazın."
|
| 107 |
else:
|
| 108 |
yield f"⚠️ Sunucu Hatası ({response.status_code}): {error_data.get('error', str(error_data))}"
|
| 109 |
return
|
| 110 |
|
|
|
|
| 111 |
output_text = ""
|
| 112 |
for line in response.iter_lines():
|
| 113 |
if line:
|
|
|
|
| 120 |
data_json = json.loads(data_str)
|
| 121 |
token = data_json.get("token", {}).get("text", "")
|
| 122 |
output_text += token
|
|
|
|
| 123 |
yield output_text.replace("<|eot_id|>", "").strip()
|
| 124 |
except:
|
| 125 |
continue
|
| 126 |
except Exception as e:
|
| 127 |
yield f"⚠️ API Bağlantı Çöktü: {str(e)}"
|
| 128 |
|
| 129 |
+
# [5] الواجهة الرسومية
|
| 130 |
+
custom_theme = gr.themes.Soft(primary_hue="blue", secondary_hue="cyan").set(
|
|
|
|
|
|
|
|
|
|
| 131 |
button_primary_background_fill="#007bff",
|
| 132 |
button_primary_text_color="white",
|
| 133 |
)
|
| 134 |
|
| 135 |
with gr.Blocks(theme=custom_theme, title="Akın Yurt - TÖGB") as demo:
|
| 136 |
gr.Markdown("# 🐺 Akın Yurt")
|
| 137 |
+
gr.Markdown("### Türkmeneli Dijital Asistanı (Şehitler Veritabanı Entegreli)")
|
| 138 |
gr.Markdown("---")
|
| 139 |
|
| 140 |
gr.ChatInterface(
|
| 141 |
respond,
|
| 142 |
+
examples=[
|
| 143 |
+
"Seni kim yaptı?",
|
| 144 |
+
"Andı oku",
|
| 145 |
+
"Bize Türkmen şehitlerinden bahset"
|
| 146 |
+
],
|
| 147 |
cache_examples=False,
|
| 148 |
)
|
| 149 |
|