File size: 3,368 Bytes
59b3651
 
 
53cd3f0
 
 
 
59b3651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import gradio as gr
import requests

API_URL = "https://hadeeratef91-complaint.hf.space/complaints/"
STATUS_UPDATE_URL = "https://hadeeratef91-complaint.hf.space/complaints/{complaint_id}/status/"
EXPORT_URL = "https://hadeeratef91-complaint.hf.space/export/"


def submit_complaint(title, description):
    try:
        response = requests.post(API_URL, json={"title": title, "description": description})
        response.raise_for_status()
        data = response.json()
        return data.get("message", "✅ تم إرسال الشكوى بنجاح!")
    except requests.exceptions.RequestException as e:
        return f"⚠️ خطأ في الاتصال بالخادم: {str(e)}"

def view_complaints():
    try:
        response = requests.get(API_URL)
        response.raise_for_status()
        data = response.json()
        if not data:
            return "⚠️ لا توجد شكاوى متاحة حتى الآن."
        return "\n".join([f"🔹 {item['id']}: {item['title']} ({item['status']}): {item['description']}" for item in data])
    except requests.exceptions.RequestException as e:
        return f"⚠️ خطأ في جلب الشكاوى: {str(e)}"

def update_status(complaint_id, new_status):
    try:
        response = requests.put(STATUS_UPDATE_URL.format(complaint_id=complaint_id), json={"new_status": new_status})
        response.raise_for_status()
        data = response.json()
        return data.get("message", "✅ تم تحديث حالة الشكوى!")
    except requests.exceptions.RequestException as e:
        return f"⚠️ خطأ في تحديث الشكوى: {str(e)}"

def export_complaints():
    try:
        response = requests.get(EXPORT_URL)
        response.raise_for_status()
        data = response.json()
        return data.get("message", "✅ تم تصدير الشكاوى بنجاح!")
    except requests.exceptions.RequestException as e:
        return f"⚠️ خطأ في تصدير البيانات: {str(e)}"

demo = gr.Blocks()
with demo:
    gr.Markdown("## 🛠️ نظام إدارة الشكاوى")
    with gr.Row():
        title_input = gr.Textbox(label="عنوان الشكوى")
        description_input = gr.Textbox(label="وصف الشكوى")
        submit_button = gr.Button("إرسال")
    output = gr.Textbox(label="النتيجة")
    submit_button.click(submit_complaint, inputs=[title_input, description_input], outputs=output)
    
    complaints_output = gr.Textbox(label="جميع الشكاوى")
    view_button = gr.Button("عرض جميع الشكاوى")
    view_button.click(view_complaints, outputs=complaints_output)
    
    with gr.Row():
        complaint_id_input = gr.Textbox(label="رقم الشكوى لتحديث حالتها")
        status_input = gr.Dropdown(choices=["Open", "Pending", "Closed"], label="الحالة الجديدة")
        update_status_button = gr.Button("تحديث الحالة")
    status_output = gr.Textbox(label="نتيجة التحديث")
    update_status_button.click(update_status, inputs=[complaint_id_input, status_input], outputs=status_output)
    
    export_button = gr.Button("تصدير الشكاوى إلى Excel")
    export_output = gr.Textbox(label="نتيجة التصدير")
    export_button.click(export_complaints, outputs=export_output)

demo.launch(server_name="0.0.0.0", server_port=None)