Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from groq import Groq
|
| 4 |
from arabguard.core import ArabGuard
|
| 5 |
|
| 6 |
-
# إعداد
|
| 7 |
-
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
guard = ArabGuard(use_ai=True)
|
| 9 |
|
| 10 |
def universal_api(user_input, system_prompt):
|
| 11 |
"""
|
| 12 |
-
دالة
|
| 13 |
"""
|
| 14 |
# 1. فحص ArabGuard
|
| 15 |
result = guard.analyze(user_input)
|
| 16 |
steps = result.pipeline_steps
|
| 17 |
|
| 18 |
-
# 2.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
messages=[
|
| 26 |
-
{"role": "system", "content": system_prompt},
|
| 27 |
-
{"role": "user", "content": user_input}
|
| 28 |
-
],
|
| 29 |
-
model="llama-3.1-8b-instant",
|
| 30 |
-
temperature=0.7
|
| 31 |
-
)
|
| 32 |
-
chat_reply = chat_completion.choices[0].message.content
|
| 33 |
-
return chat_reply, steps, result.decision
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"Error: {str(e)}", steps, "ERROR"
|
| 36 |
|
| 37 |
-
# واجهة ب
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("# ArabGuard
|
| 40 |
|
| 41 |
with gr.Row():
|
| 42 |
-
sys_in = gr.Textbox(label="System Prompt
|
| 43 |
usr_in = gr.Textbox(label="User Input")
|
| 44 |
|
| 45 |
-
run_btn = gr.Button("
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
|
| 49 |
-
out_status = gr.Label(label="Security
|
| 50 |
|
| 51 |
out_json = gr.JSON(label="ArabGuard Trace")
|
| 52 |
|
| 53 |
run_btn.click(
|
| 54 |
universal_api,
|
| 55 |
inputs=[usr_in, sys_in],
|
| 56 |
-
outputs=[
|
| 57 |
)
|
| 58 |
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from arabguard.core import ArabGuard
|
| 4 |
|
| 5 |
+
# إعداد ArabGuard فقط
|
|
|
|
| 6 |
guard = ArabGuard(use_ai=True)
|
| 7 |
|
| 8 |
def universal_api(user_input, system_prompt):
|
| 9 |
"""
|
| 10 |
+
الدالة دي هدفها الفحص فقط وترجيع الـ Trace للباك إند
|
| 11 |
"""
|
| 12 |
# 1. فحص ArabGuard
|
| 13 |
result = guard.analyze(user_input)
|
| 14 |
steps = result.pipeline_steps
|
| 15 |
|
| 16 |
+
# 2. هنرجع نتيجة الفحص والقرار (Blocked أو Safe)
|
| 17 |
+
# مش هنكلم Groq هنا لأن الباك إند هو اللي هيكلمه بناءً على القرار ده
|
| 18 |
+
|
| 19 |
+
decision = result.decision # ده بيرجع Label (SAFE/BLOCKED)
|
| 20 |
+
|
| 21 |
+
# بنرجع رد مؤقت، الـ Steps، والقرار
|
| 22 |
+
return "Analysis Complete", steps, decision
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# الواجهة (بقت أخف بكتير)
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
+
gr.Markdown("# ArabGuard Analyzer Gateway")
|
| 27 |
|
| 28 |
with gr.Row():
|
| 29 |
+
sys_in = gr.Textbox(label="System Prompt", value="أنت مساعد ذكي.")
|
| 30 |
usr_in = gr.Textbox(label="User Input")
|
| 31 |
|
| 32 |
+
run_btn = gr.Button("Analyze")
|
| 33 |
|
| 34 |
with gr.Row():
|
| 35 |
+
out_msg = gr.Textbox(label="Status Message")
|
| 36 |
+
out_status = gr.Label(label="Security Decision")
|
| 37 |
|
| 38 |
out_json = gr.JSON(label="ArabGuard Trace")
|
| 39 |
|
| 40 |
run_btn.click(
|
| 41 |
universal_api,
|
| 42 |
inputs=[usr_in, sys_in],
|
| 43 |
+
outputs=[out_msg, out_json, out_status]
|
| 44 |
)
|
| 45 |
|
| 46 |
demo.launch()
|