Verianchor commited on
Commit
6fb8996
·
verified ·
1 Parent(s): 6366161

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -41
app.py CHANGED
@@ -1,52 +1,43 @@
1
  import gradio as gr
2
- import requests
3
 
4
- # موديل Zephyr: سريع جداً، ذكي، ومتاح دايماً للـ Inference المجاني
5
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
6
-
7
- def chat_with_iam(message, history):
8
- # --- [IAM DGT Layer: Subtle Safety] ---
9
- # فحص سريع للمدخلات (اختياري)
10
- forbidden_terms = ["غراء في البيتزا", "glue on pizza"]
11
- if any(term in message.lower() for term in forbidden_terms):
12
- return "⚠️ [IAM Block]: VeriAnchor detected a dangerous hallucination pattern. Access Denied."
13
-
14
- # --- [Calling the AI Brain] ---
15
- payload = {
16
- "inputs": f"<|system|>\nYou are VeriAnchor AI, a safe and deterministic assistant.</s>\n<|user|>\n{message}</s>\n<|assistant|>\n",
17
- "parameters": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95}
18
- }
 
 
19
 
20
  try:
21
- response = requests.post(API_URL, json=payload, timeout=15)
22
- result = response.json()
23
-
24
- # إذا كان الموديل لسه بيحمل (Hugging Face Loading)
25
- if isinstance(result, dict) and "error" in result:
26
- return f"🛡️ [IAM Shield]: الموديل بيجهز نفسه حالياً (Loading). جرب تاني كمان 20 ثانية."
27
-
28
- # استخراج النص بنجاح
29
- if isinstance(result, list) and len(result) > 0:
30
- full_text = result[0].get('generated_text', "")
31
- # تنظيف الرد لإظهار إجابة الموديل فقط
32
- clean_answer = full_text.split("<|assistant|>\n")[-1].strip()
33
 
34
- if not clean_answer:
35
- return "❌ [IAM Shield]: الرد المولد غير كافٍ للتوثيق."
36
-
37
- return f"{clean_answer}\n\n⚓ [Secured by VeriAnchor IAM Protocol]"
38
-
39
- return "❌ [IAM Shield]: فشل في الحصول على رد موثق."
40
 
41
- except Exception as e:
42
- return f"⚠️ [System Alert]: VeriAnchor is monitoring a connection issue. Details: {str(e)[:50]}"
43
 
44
- # واجهة مستخدم احترافية
45
  demo = gr.ChatInterface(
46
- fn=chat_with_iam,
47
- title="⚓ VeriAnchor AI v2",
48
- description="Your professional AI assistant, protected by IAM Protocol (Deterministic Safety).",
49
- theme="default"
50
  )
51
 
52
  if __name__ == "__main__":
 
1
  import gradio as gr
 
2
 
3
+ # الربط المباشر بالموديل - دي أكثر طريقة مستقرة بتمنع الـ Runtime Error
4
+ # الموديل ده "Zephyr" هو النسخة المطورة والذكية جداً
5
+ try:
6
+ client = gr.load("models/HuggingFaceH4/zephyr-7b-beta")
7
+ except Exception as e:
8
+ client = None
9
+
10
+ def verianchor_iam_engine(message, history):
11
+ # 1. طبقة الـ IAM: فحص المدخلات (DGT Layer)
12
+ # حماية من الهلوسة الخطيرة (زي مثال البيتزا اللي في بحثك)
13
+ danger_zone = ["غراء", "glue", "بيتزا", "pizza"]
14
+ if any(word in message.lower() for word in danger_zone):
15
+ return "⚠️ [IAM Block]: تم رصد نمط هلوسة خطر. بروتوكول VeriAnchor يمنع الرد لحمايتك."
16
+
17
+ # 2. الاتصال بالموديل (The Brain)
18
+ if client is None:
19
+ return "❌ [System Error]: السيرفر بيعمل تحديث، جرب كمان لحظات."
20
 
21
  try:
22
+ # الموديل بيجاوب هنا بذكاء زي ChatGPT
23
+ response = client(message)
24
+
25
+ # 3. طبقة الـ IAM: توثيق المخرجات (DAC Layer)
26
+ # التأكد إن الرد سليم ومش مجرد تهنيج
27
+ if not response or len(str(response)) < 5:
28
+ return "❌ [IAM Shield]: الرد غير موثق رياضياً. تم تفعيل بروتوكول السكوت."
 
 
 
 
 
29
 
30
+ return f"{response}\n\n⚓ [Secured by VeriAnchor IAM Protocol]"
 
 
 
 
 
31
 
32
+ except Exception:
33
+ return "🛡️ [IAM Monitoring]: الموديل مضغوط حالياً، لكن بروتوكول VeriAnchor شغال وبيحمي اتصالك. حاول تاني."
34
 
35
+ # واجهة شات احترافية
36
  demo = gr.ChatInterface(
37
+ fn=verianchor_iam_engine,
38
+ title="⚓ VeriAnchor AI Pro",
39
+ description="The First Deterministic Safety Layer for AI - Powered by IAM Protocol",
40
+ theme="soft"
41
  )
42
 
43
  if __name__ == "__main__":