File size: 3,773 Bytes
9009308
 
 
 
 
 
 
e55ac63
619adc6
 
 
 
 
e55ac63
 
619adc6
 
 
 
e670d75
9009308
 
 
 
 
 
 
 
 
 
 
 
68fc69b
 
e670d75
 
 
 
 
68fc69b
619adc6
 
 
 
 
68fc69b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9009308
68fc69b
9009308
 
68fc69b
f8dc2b9
 
 
 
 
68fc69b
 
 
 
 
f8dc2b9
68fc69b
 
 
 
 
 
 
 
f8dc2b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9009308
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
import requests
from datetime import datetime
import os
from dotenv import load_dotenv

load_dotenv()

SUPABASE_URL = "https://aumllmcpfqpfeunedemp.supabase.co"
TABLE_NAME = "feedback"
API_KEY = os.getenv("SUPERBASE_KEY",)
SUPABASE_KEY = API_KEY
ADMIN_PASSWORD = os.getenv("SUPERBASE_KEY_ADMIN",)

def submit_feedback(app_id, rating, comment):
    # SUPABASE_URL = "https://aumllmcpfqpfeunedemp.supabase.co"
    # TABLE_NAME = "feedback"
    # API_KEY = os.getenv("SUPERBASE_KEY",)
    # SUPABASE_KEY = API_KEY
    app_id = APP_ID
    payload = {
        "app_id": app_id,
        "rating": rating,
        "comment": comment,
        "created_at": datetime.utcnow().isoformat()
    }
    headers = {
        "apikey": SUPABASE_KEY,
        "Authorization": f"Bearer {SUPABASE_KEY}",
        "Content-Type": "application/json"
    }
    r = requests.post(f"{SUPABASE_URL}/rest/v1/{TABLE_NAME}", json=payload, headers=headers)
    return "Thank you!" if r.status_code == 201 else f"Error: {r.status_code}"

APP_ID = None
def load_feedback(app_id):  # This is the function triggered by `gr.load`
        APP_ID = app_id
        return f"Loaded for {app_id}"

def fetch_feedback(app_filter, pwd):
    # SUPABASE_URL = "https://aumllmcpfqpfeunedemp.supabase.co"
    # API_KEY = os.getenv("SUPERBASE_KEY",)
    # TABLE_NAME = "feedback"
    # SUPABASE_KEY = API_KEY
    # ADMIN_PASSWORD = os.getenv("SUPERBASE_KEY_ADMIN",)
    if pwd != ADMIN_PASSWORD:
        return "Unauthorized", []
    headers = {
        "apikey": SUPABASE_KEY,
        "Authorization": f"Bearer {SUPABASE_KEY}"
    }
    query = f"?select=app_id,rating,comment,created_at&order=created_at.desc"
    if app_filter:
        query += f"&app_id=eq.{app_filter}"
    url = f"{SUPABASE_URL}/rest/v1/{TABLE_NAME}{query}"
    r = requests.get(url, headers=headers)
    if r.status_code == 200:
        data = r.json()
        rows = [[d["app_id"], d["rating"], d["comment"], d["created_at"]] for d in data]
        return f"{len(rows)} entries", rows
    else:
        return f"Error: {r.status_code}", []

with gr.Blocks() as demo:
    with gr.Tab("Feedback Form"):
        gr.Markdown("### Rate this App")

        app_id = gr.Textbox(label="App ID", visible=False)
        app_id_display = gr.Markdown("")  # optional display

        rating = gr.Slider(minimum=1, maximum=5, step=1, label="Rating")
        comment = gr.Textbox(label="Comment", lines=4)
        output = gr.Textbox(label="Status", interactive=False)
        submit_btn = gr.Button("Submit")
        submit_btn.click(fn=submit_feedback, inputs=[app_id, rating, comment], outputs=[output])
        
    with gr.Tab("Admin Panel"):
        gr.Markdown("### View Feedback (Admin Only)")
        pwd = gr.Textbox(label="Admin Password", type="password")
        app_filter = gr.Textbox(label="Filter by App ID (optional)")
        status = gr.Textbox(label="Status")
        table = gr.Dataframe(headers=["App ID", "Rating", "Comment", "Timestamp"], interactive=False)
        view_btn = gr.Button("Load Feedback")
        view_btn.click(fn=fetch_feedback, inputs=[app_filter, pwd], outputs=[status, table])
gr.HTML("""
<script>
window.addEventListener("load", function () {
    const params = new URLSearchParams(window.location.search);
    const appId = params.get("app_id");
    if (appId) {
        const inputBox = document.querySelector('input[type="text"][aria-label="App ID"]');
        if (inputBox) {
            inputBox.value = appId;
            inputBox.dispatchEvent(new Event('input', { bubbles: true }));
        }
        const display = document.querySelector("div.markdown");
        if (display) {
            display.innerHTML = "**App ID:** " + appId;
        }
    }
});
</script>
""")

demo.launch()