Spaces:
Sleeping
Sleeping
| 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() | |