Spaces:
Runtime error

new1 / interface.py
killuazide's picture
Create interface.py
39aa368 verified
Raw History Blame Contribute Delete
10.5 kB
import gradio as gr
import requests
import json
import logging
from datetime import datetime
import time
# إعداد التسجيل
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# وظيفة تسجيل الدخول
def login(username, password):
try:
response = requests.post("http://localhost:8000/login", data={
"username": username,
"password": password
})
if response.status_code == 200:
token = response.json()["access_token"]
role = response.json().get("role", "viewer")
return f"تم تسجيل الدخول بنجاح! الدور: {role}", token
else:
return "خطأ في تسجيل الدخول", None
except Exception as e:
logger.error(f"Login error: {str(e)}")
return f"خطأ في الاتصال: {str(e)}", None
# وظيفة حفظ المحادثة
def save_conversation(token, input_type, input_content, output_content, risk_level, confidence):
headers = {"Authorization": f"Bearer {token}"}
response = requests.post("http://localhost:8000/save_conversation", json={
"input_type": input_type,
"input_content": input_content,
"output_content": output_content,
"risk_level": risk_level,
"confidence_score": float(confidence) if confidence else None
}, headers=headers)
return response.json()
# وظيفة رفع ملف PDF
def upload_pdf(file, token):
headers = {"Authorization": f"Bearer {token}"}
files = {"file": (file.name, open(file.name, "rb"), "application/pdf")}
response = requests.post("http://localhost:8000/upload_pdf", files=files, headers=headers)
return response.json()
# وظيفة رفع ملف Excel
def upload_excel(file, token):
headers = {"Authorization": f"Bearer {token}"}
files = {"file": (file.name, open(file.name, "rb"), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")}
response = requests.post("http://localhost:8000/upload_excel", files=files, headers=headers)
return response.json()
# وظيفة رفع ملف صوت
def upload_audio(file, token):
headers = {"Authorization": f"Bearer {token}"}
files = {"file": (file.name, open(file.name, "rb"), "audio/mpeg")}
response = requests.post("http://localhost:8000/upload_audio", files=files, headers=headers)
return response.json()
# وظيفة إضافة تقييم
def add_feedback(token, conversation_id, actual_result, user_feedback, rating):
headers = {"Authorization": f"Bearer {token}"}
response = requests.post("http://localhost:8000/add_feedback", json={
"conversation_id": int(conversation_id),
"actual_result": actual_result,
"user_feedback": user_feedback,
"rating": int(rating) if rating else None
}, headers=headers)
return response.json()
# وظيفة إنشاء حساب جديد
def register_user(username, password, email, role, first_name, last_name):
try:
response = requests.post("http://localhost:8000/register", json={
"username": username,
"password": password,
"email": email,
"role": role,
"first_name": first_name,
"last_name": last_name
})
return response.json()
except Exception as e:
return {"error": str(e)}
# وظيفة إنشاء نسخة احتياطية
def create_backup(token):
headers = {"Authorization": f"Bearer {token}"}
response = requests.post("http://localhost:8000/create_backup", headers=headers)
return response.json()
# وظيفة تحديث النظام
def update_system(token):
headers = {"Authorization": f"Bearer {token}"}
response = requests.post("http://localhost:8000/update_system", headers=headers)
return response.json()
# وظيفة الحصول على إحصائيات النظام
def get_system_stats():
response = requests.get("http://localhost:8000/system_stats")
return response.json()
# وظيفة البحث في المحادثات
def search_conversations(query, token):
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(f"http://localhost:8000/search/conversations?query={query}", headers=headers)
return response.json()
# وظيفة الحصول على محادثات المستخدم
def get_user_conversations(token):
headers = {"Authorization": f"Bearer {token}"}
response = requests.get("http://localhost:8000/user/conversations", headers=headers)
return response.json()
# واجهة Gradio
with gr.Blocks(title="MyAIProject_v2") as demo:
gr.Markdown("# 🧠 نظام الذكاء الاصطناعي MyAIProject_v2")
gr.Markdown("## نظام تحليلي متقدم للبيانات المختلفة")
with gr.Tab("تسجيل الدخول"):
with gr.Row():
username_login = gr.Textbox(label="اسم المستخدم")
password_login = gr.Textbox(label="كلمة المرور", type="password")
login_btn = gr.Button("تسجيل الدخول")
with gr.Row():
login_output = gr.Textbox(label="رسالة التسجيل", interactive=False)
token_output = gr.Textbox(label="Token", visible=False, interactive=False)
with gr.Tab("التسجيل"):
with gr.Row():
username_reg = gr.Textbox(label="اسم المستخدم")
password_reg = gr.Textbox(label="كلمة المرور", type="password")
email_reg = gr.Textbox(label="البريد الإلكتروني")
first_name_reg = gr.Textbox(label="الاسم الأول")
last_name_reg = gr.Textbox(label="الاسم الأخير")
role_reg = gr.Dropdown(["admin", "analyst", "viewer"], value="viewer", label="الدور")
register_btn = gr.Button("تسجيل")
with gr.Row():
register_output = gr.Textbox(label="رسالة التسجيل", interactive=False)
with gr.Tab("المحادثة"):
with gr.Row():
input_type = gr.Dropdown(["text", "image", "audio", "video"], value="text", label="نوع المدخل")
input_content = gr.Textbox(label="المحتوى", lines=5)
output_content = gr.Textbox(label="النتيجة", lines=5)
risk_level = gr.Dropdown(["Low", "Medium", "High"], value="Medium", label="مستوى المخاطر")
confidence = gr.Slider(0, 1, value=0.8, label="درجة الثقة")
save_btn = gr.Button("حفظ المحادثة")
with gr.Row():
save_output = gr.Textbox(label="النتيجة", interactive=False)
with gr.Tab("رفع الملفات"):
with gr.Tab("PDF"):
with gr.Row():
pdf_file = gr.File(label="اختر ملف PDF")
upload_pdf_btn = gr.Button("رفع PDF")
with gr.Row():
pdf_output = gr.Textbox(label="نتيجة الرفع", interactive=False)
with gr.Tab("Excel"):
with gr.Row():
excel_file = gr.File(label="اختر ملف Excel")
upload_excel_btn = gr.Button("رفع Excel")
with gr.Row():
excel_output = gr.Textbox(label="نتيجة الرفع", interactive=False)
with gr.Tab("صوت"):
with gr.Row():
audio_file = gr.File(label="اختر ملف صوت")
upload_audio_btn = gr.Button("رفع صوت")
with gr.Row():
audio_output = gr.Textbox(label="نتيجة الرفع", interactive=False)
with gr.Tab("التقييم"):
with gr.Row():
conversation_id = gr.Number(label="معرف المحادثة")
actual_result = gr.Textbox(label="النتيجة الحقيقية")
user_feedback = gr.Textbox(label="التقييم", lines=3)
rating = gr.Slider(1, 5, value=3, label="التقييم (1-5)")
feedback_btn = gr.Button("إضافة تقييم")
with gr.Row():
feedback_output = gr.Textbox(label="النتيجة", interactive=False)
with gr.Tab("البحث"):
with gr.Row():
search_query = gr.Textbox(label="كلمة البحث")
search_btn = gr.Button("بحث")
with gr.Row():
search_output = gr.JSON(label="نتائج البحث")
with gr.Tab("الإحصائيات"):
with gr.Row():
stats_btn = gr.Button("عرض الإحصائيات")
with gr.Row():
stats_output = gr.JSON(label="الإحصائيات")
with gr.Tab("الإدارة"):
with gr.Row():
backup_btn = gr.Button("إنشاء نسخة احتياطية")
update_btn = gr.Button("تحديث النظام")
with gr.Row():
backup_output = gr.Textbox(label="نتيجة النسخ الاحتياطي", interactive=False)
update_output = gr.Textbox(label="نتيجة التحديث", interactive=False)
# ربط الأحداث
login_btn.click(login, inputs=[username_login, password_login], outputs=[login_output, token_output])
register_btn.click(register_user, inputs=[username_reg, password_reg, email_reg, role_reg, first_name_reg, last_name_reg], outputs=register_output)
save_btn.click(save_conversation, inputs=[token_output, input_type, input_content, output_content, risk_level, confidence], outputs=save_output)
upload_pdf_btn.click(upload_pdf, inputs=[pdf_file, token_output], outputs=pdf_output)
upload_excel_btn.click(upload_excel, inputs=[excel_file, token_output], outputs=excel_output)
upload_audio_btn.click(upload_audio, inputs=[audio_file, token_output], outputs=audio_output)
feedback_btn.click(add_feedback, inputs=[token_output, conversation_id, actual_result, user_feedback, rating], outputs=feedback_output)
stats_btn.click(get_system_stats, outputs=stats_output)
backup_btn.click(create_backup, inputs=[token_output], outputs=backup_output)
update_btn.click(update_system, inputs=[token_output], outputs=update_output)
search_btn.click(search_conversations, inputs=[search_query, token_output], outputs=search_output)
demo.launch()