AnjaliRai78 commited on
Commit
10f1481
1 Parent(s): 8b5c615

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import time
4
+ import os
5
+ from dotenv import load_dotenv
6
+ from huggingface_hub import list_models
7
+
8
+ load_dotenv()
9
+ API_URL = os.getenv("API_URL")
10
+ class CreateProfile:
11
+ def __init__(self, _id=None):
12
+ self._id = _id
13
+ self.name = ""
14
+ self.username = ""
15
+
16
+ newprofile = CreateProfile()
17
+
18
+ def check_active_users():
19
+ response = requests.get(f"{API_URL}/status")
20
+ if response.status_code == 200:
21
+ queue_length = response.json().get("queue_length", 0)
22
+ if queue_length > 2:
23
+ return False, "There's currently a high volume of requests. The server is busy processing these requests. Please try again later."
24
+ return True, ""
25
+
26
+ def process_ui_image(image, user_task, progress=gr.Progress()):
27
+ progress(0)
28
+
29
+ # Check the number of active users
30
+ can_proceed, message = check_active_users()
31
+ if not can_proceed:
32
+ return message
33
+
34
+ # Upload image and user task to the API
35
+ files = {'image': open(image, 'rb')}
36
+ data = {'user_task': user_task, "name": newprofile.name, "username": newprofile.username}
37
+ response = requests.post(f"{API_URL}/process", files=files, data=data)
38
+
39
+ if response.status_code != 200:
40
+ return f"Error: {response.text}"
41
+
42
+ # Get the request ID from the response
43
+ request_id = response.json().get("request_id")
44
+ print("request_id",request_id)
45
+
46
+ if not request_id:
47
+ return "Error: Failed to get request ID from the server."
48
+
49
+ # Poll the status of the request
50
+ i = 0.01
51
+ while True:
52
+ status_response = requests.get(f"{API_URL}/status/{request_id}")
53
+ status = status_response.json()
54
+
55
+ if status.get("status") == "completed":
56
+ progress(1)
57
+ break
58
+ elif status.get("status") == "failed":
59
+ return "Error: Processing failed."
60
+
61
+ progress(i)
62
+ i += 0.01
63
+ if i >= 0.9:
64
+ i = 0.9
65
+ time.sleep(1)
66
+
67
+ # Download the final PDF
68
+ final_pdf_response = requests.get(f"{API_URL}/get_file")
69
+
70
+ if final_pdf_response.status_code == 200:
71
+ with open("UXEva_report.pdf", "wb") as f:
72
+ f.write(final_pdf_response.content)
73
+ return "UXEva_report.pdf"
74
+ else:
75
+ return "Error: Failed to download the PDF."
76
+
77
+ def update_submit_button(image, user_task):
78
+ if not image or not user_task:
79
+ return gr.update(interactive=False), gr.update(visible=True)
80
+ return gr.update(interactive=True), gr.update(visible=False)
81
+
82
+
83
+ with gr.Blocks(css="""
84
+ .image-preview img {max-height: 400px; max-width: 400px;}
85
+ .disabled {cursor: not-allowed; color: red;}
86
+ """) as demo:
87
+ gr.Markdown("""
88
+ <h1 style="text-align: center;">
89
+ <span style="color: green;">UXEva</span>: UI/UX Evaluation Tool Powered by LLM Agent
90
+ </h1>
91
+ """)
92
+ gr.LoginButton()
93
+
94
+ with gr.Column(visible=False) as main_ui:
95
+ image_input = gr.Image(type="filepath", label="Upload UI Image", elem_classes="image-preview")
96
+ user_task_input = gr.Textbox(label="Enter User Task")
97
+ pdf_output = gr.File(label="Download PDF Report")
98
+ submit_btn = gr.Button("Submit", interactive=False)
99
+ warning = gr.Markdown("<span class='disabled'>Upload image and enter user task</span>", visible=False)
100
+
101
+ image_input.change(update_submit_button, inputs=[image_input, user_task_input], outputs=[submit_btn, warning])
102
+ user_task_input.change(update_submit_button, inputs=[image_input, user_task_input], outputs=[submit_btn, warning])
103
+
104
+ submit_btn.click(
105
+ process_ui_image,
106
+ inputs=[image_input, user_task_input],
107
+ outputs=[pdf_output]
108
+ )
109
+ def get_profile(profile: gr.OAuthProfile):
110
+ global newprofile
111
+ if profile is None:
112
+ print("unknow user.")
113
+ else:
114
+ newprofile.name = profile.name
115
+ newprofile.username = profile.username
116
+ print("profile name", profile.name)
117
+ print("profile username", profile.username)
118
+ return {main_ui: gr.Column(visible=True)}
119
+
120
+ demo.load(get_profile, inputs=None, outputs=[main_ui])
121
+
122
+ demo.queue(default_concurrency_limit=1, max_size=7)
123
+ demo.launch()