Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +52 -0
- interface.py +71 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# نموذج بيانات الشكوى
|
| 9 |
+
class Complaint(BaseModel):
|
| 10 |
+
title: str
|
| 11 |
+
description: str
|
| 12 |
+
status: str = "Open"
|
| 13 |
+
|
| 14 |
+
# قائمة لتخزين الشكاوى مؤقتًا (بديل عن قاعدة بيانات)
|
| 15 |
+
complaints_db = []
|
| 16 |
+
complaint_id_counter = 1
|
| 17 |
+
|
| 18 |
+
@app.post("/complaints/")
|
| 19 |
+
def create_complaint(complaint: Complaint):
|
| 20 |
+
global complaint_id_counter
|
| 21 |
+
new_complaint = {
|
| 22 |
+
"id": complaint_id_counter,
|
| 23 |
+
"title": complaint.title,
|
| 24 |
+
"description": complaint.description,
|
| 25 |
+
"status": complaint.status
|
| 26 |
+
}
|
| 27 |
+
complaints_db.append(new_complaint)
|
| 28 |
+
complaint_id_counter += 1
|
| 29 |
+
return {"message": "✅ تم إرسال الشكوى بنجاح!", "complaint": new_complaint}
|
| 30 |
+
|
| 31 |
+
@app.get("/complaints/", response_model=List[dict])
|
| 32 |
+
def get_complaints():
|
| 33 |
+
if not complaints_db:
|
| 34 |
+
return []
|
| 35 |
+
return complaints_db
|
| 36 |
+
|
| 37 |
+
@app.put("/complaints/{complaint_id}/status/")
|
| 38 |
+
def update_complaint_status(complaint_id: int, new_status: str):
|
| 39 |
+
for complaint in complaints_db:
|
| 40 |
+
if complaint["id"] == complaint_id:
|
| 41 |
+
complaint["status"] = new_status
|
| 42 |
+
return {"message": "✅ تم تحديث حالة الشكوى!"}
|
| 43 |
+
raise HTTPException(status_code=404, detail="⚠️ الشكوى غير موجودة")
|
| 44 |
+
|
| 45 |
+
@app.get("/export/")
|
| 46 |
+
def export_complaints():
|
| 47 |
+
if not complaints_db:
|
| 48 |
+
raise HTTPException(status_code=404, detail="⚠️ لا توجد بيانات للتصدير.")
|
| 49 |
+
|
| 50 |
+
df = pd.DataFrame(complaints_db)
|
| 51 |
+
df.to_csv("complaints_export.csv", index=False)
|
| 52 |
+
return {"message": "✅ تم تصدير الشكاوى بنجاح! يمكنك تحميل الملف من السيرفر."}
|
interface.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
API_URL = "https://your-huggingface-space-url.hf.space/complaints/"
|
| 5 |
+
STATUS_UPDATE_URL = "https://your-huggingface-space-url.hf.space/complaints/{complaint_id}/status/"
|
| 6 |
+
EXPORT_URL = "https://your-huggingface-space-url.hf.space/export/"
|
| 7 |
+
|
| 8 |
+
def submit_complaint(title, description):
|
| 9 |
+
try:
|
| 10 |
+
response = requests.post(API_URL, json={"title": title, "description": description})
|
| 11 |
+
response.raise_for_status()
|
| 12 |
+
data = response.json()
|
| 13 |
+
return data.get("message", "✅ تم إرسال الشكوى بنجاح!")
|
| 14 |
+
except requests.exceptions.RequestException as e:
|
| 15 |
+
return f"⚠️ خطأ في الاتصال بالخادم: {str(e)}"
|
| 16 |
+
|
| 17 |
+
def view_complaints():
|
| 18 |
+
try:
|
| 19 |
+
response = requests.get(API_URL)
|
| 20 |
+
response.raise_for_status()
|
| 21 |
+
data = response.json()
|
| 22 |
+
if not data:
|
| 23 |
+
return "⚠️ لا توجد شكاوى متاحة حتى الآن."
|
| 24 |
+
return "\n".join([f"🔹 {item['id']}: {item['title']} ({item['status']}): {item['description']}" for item in data])
|
| 25 |
+
except requests.exceptions.RequestException as e:
|
| 26 |
+
return f"⚠️ خطأ في جلب الشكاوى: {str(e)}"
|
| 27 |
+
|
| 28 |
+
def update_status(complaint_id, new_status):
|
| 29 |
+
try:
|
| 30 |
+
response = requests.put(STATUS_UPDATE_URL.format(complaint_id=complaint_id), json={"new_status": new_status})
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
data = response.json()
|
| 33 |
+
return data.get("message", "✅ تم تحديث حالة الشكوى!")
|
| 34 |
+
except requests.exceptions.RequestException as e:
|
| 35 |
+
return f"⚠️ خطأ في تحديث الشكوى: {str(e)}"
|
| 36 |
+
|
| 37 |
+
def export_complaints():
|
| 38 |
+
try:
|
| 39 |
+
response = requests.get(EXPORT_URL)
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
data = response.json()
|
| 42 |
+
return data.get("message", "✅ تم تصدير الشكاوى بنجاح!")
|
| 43 |
+
except requests.exceptions.RequestException as e:
|
| 44 |
+
return f"⚠️ خطأ في تصدير البيانات: {str(e)}"
|
| 45 |
+
|
| 46 |
+
demo = gr.Blocks()
|
| 47 |
+
with demo:
|
| 48 |
+
gr.Markdown("## 🛠️ نظام إدارة الشكاوى")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
title_input = gr.Textbox(label="عنوان الشكوى")
|
| 51 |
+
description_input = gr.Textbox(label="وصف الشكوى")
|
| 52 |
+
submit_button = gr.Button("إرسال")
|
| 53 |
+
output = gr.Textbox(label="النتيجة")
|
| 54 |
+
submit_button.click(submit_complaint, inputs=[title_input, description_input], outputs=output)
|
| 55 |
+
|
| 56 |
+
complaints_output = gr.Textbox(label="جميع الشكاوى")
|
| 57 |
+
view_button = gr.Button("عرض جميع الشكاوى")
|
| 58 |
+
view_button.click(view_complaints, outputs=complaints_output)
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
complaint_id_input = gr.Textbox(label="رقم الشكوى لتحديث حالتها")
|
| 62 |
+
status_input = gr.Dropdown(choices=["Open", "Pending", "Closed"], label="الحالة الجديدة")
|
| 63 |
+
update_status_button = gr.Button("تحديث الحالة")
|
| 64 |
+
status_output = gr.Textbox(label="نتيجة التحديث")
|
| 65 |
+
update_status_button.click(update_status, inputs=[complaint_id_input, status_input], outputs=status_output)
|
| 66 |
+
|
| 67 |
+
export_button = gr.Button("تصدير الشكاوى إلى Excel")
|
| 68 |
+
export_output = gr.Textbox(label="نتيجة التصدير")
|
| 69 |
+
export_button.click(export_complaints, outputs=export_output)
|
| 70 |
+
|
| 71 |
+
demo.launch(server_name="0.0.0.0", server_port=None)
|