Kuberwastaken commited on
Commit
62058a4
·
1 Parent(s): 8e26783
Files changed (1) hide show
  1. gradio_app.py +78 -52
gradio_app.py CHANGED
@@ -1,21 +1,20 @@
1
  # gradio_app.py
2
  import gradio as gr
3
  from model.analyzer import analyze_content
 
4
  import time
5
 
6
  # Custom CSS for styling
7
  custom_css = """
8
- .container {
9
- max-width: 800px;
10
- margin: 0 auto;
11
- padding: 20px;
12
  }
13
 
14
  .treat-title {
15
  text-align: center;
16
  padding: 20px;
17
  margin-bottom: 20px;
18
- background: linear-gradient(135deg, #fce4ec 0%, #e3f2fd 100%);
19
  border-radius: 15px;
20
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
21
  }
@@ -37,68 +36,84 @@ custom_css = """
37
  font-weight: bold;
38
  }
39
 
40
- .content-area {
41
- background: rgba(255, 255, 255, 0.9);
42
- border-radius: 15px;
43
- padding: 20px;
44
- margin: 20px 0;
45
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
46
- }
47
-
48
- .results-area {
49
- background: rgba(255, 255, 255, 0.9);
50
- border-radius: 15px;
51
- padding: 20px;
52
- margin-top: 20px;
53
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
54
  }
55
 
56
- .gradio-container {
57
- background: linear-gradient(135deg, #fce4ec 0%, #e3f2fd 100%) !important;
 
 
 
 
 
58
  }
59
 
60
- #analyze-btn {
61
  background-color: #d81b60 !important;
62
  color: white !important;
 
63
  border-radius: 25px !important;
64
  padding: 10px 20px !important;
65
  font-size: 1.1em !important;
66
  transition: transform 0.2s !important;
 
67
  }
68
 
69
- #analyze-btn:hover {
70
  transform: scale(1.05) !important;
 
 
 
 
 
 
 
 
71
  }
72
  """
73
 
74
  def analyze_with_loading(text, progress=gr.Progress()):
 
 
 
75
  # Initialize progress
76
  progress(0, desc="Starting analysis...")
77
 
78
- # Simulate initial loading (model preparation)
79
  for i in range(30):
80
- time.sleep(0.1)
81
  progress((i + 1) / 100)
82
 
83
- # Perform actual analysis
84
  progress(0.3, desc="Processing text...")
85
- result = analyze_content(text)
 
 
 
 
 
86
 
87
- # Simulate final processing
88
  for i in range(70, 100):
89
- time.sleep(0.05)
90
  progress((i + 1) / 100)
91
 
92
- # Format the results for display
93
  triggers = result["detected_triggers"]
94
  if triggers == ["None"]:
95
- return "No triggers detected in the content."
96
  else:
97
  trigger_list = "\n".join([f"• {trigger}" for trigger in triggers])
98
- return f"Triggers Detected:\n{trigger_list}"
99
 
100
- with gr.Blocks(css=custom_css) as iface:
101
- # Title section using HTML
 
102
  gr.HTML("""
103
  <div class="treat-title">
104
  <h1>TREAT</h1>
@@ -110,26 +125,31 @@ with gr.Blocks(css=custom_css) as iface:
110
  </div>
111
  """)
112
 
113
- with gr.Column(elem_classes="content-area"):
114
- input_text = gr.Textbox(
115
- label="Content to Analyze",
116
- placeholder="Paste your content here...",
117
- lines=8
118
- )
 
 
119
 
120
- # Analysis button
121
- analyze_btn = gr.Button(
122
- "Analyze Content",
123
- elem_id="analyze-btn"
124
- )
125
-
126
- with gr.Column(elem_classes="results-area"):
127
- output_text = gr.Textbox(
128
- label="Analysis Results",
129
- lines=5,
130
- interactive=False # This replaces 'readonly'
131
  )
132
 
 
 
 
 
 
 
 
 
 
133
  # Set up the click event
134
  analyze_btn.click(
135
  fn=analyze_with_loading,
@@ -139,4 +159,10 @@ with gr.Blocks(css=custom_css) as iface:
139
  )
140
 
141
  if __name__ == "__main__":
142
- iface.launch()
 
 
 
 
 
 
 
1
  # gradio_app.py
2
  import gradio as gr
3
  from model.analyzer import analyze_content
4
+ import asyncio
5
  import time
6
 
7
  # Custom CSS for styling
8
  custom_css = """
9
+ .gradio-container {
10
+ background: linear-gradient(135deg, #fce4ec 0%, #e3f2fd 100%) !important;
 
 
11
  }
12
 
13
  .treat-title {
14
  text-align: center;
15
  padding: 20px;
16
  margin-bottom: 20px;
17
+ background: rgba(255, 255, 255, 0.9);
18
  border-radius: 15px;
19
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
20
  }
 
36
  font-weight: bold;
37
  }
38
 
39
+ .content-area, .results-area {
40
+ background: rgba(255, 255, 255, 0.9) !important;
41
+ border-radius: 15px !important;
42
+ padding: 20px !important;
43
+ margin: 20px 0 !important;
44
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
 
 
 
 
 
 
 
 
45
  }
46
 
47
+ /* Input/Output styling */
48
+ .gradio-textbox textarea {
49
+ background-color: white !important;
50
+ color: #333 !important;
51
+ border: 1px solid #ddd !important;
52
+ border-radius: 8px !important;
53
+ padding: 10px !important;
54
  }
55
 
56
+ .gradio-button {
57
  background-color: #d81b60 !important;
58
  color: white !important;
59
+ border: none !important;
60
  border-radius: 25px !important;
61
  padding: 10px 20px !important;
62
  font-size: 1.1em !important;
63
  transition: transform 0.2s !important;
64
+ margin: 10px 0 !important;
65
  }
66
 
67
+ .gradio-button:hover {
68
  transform: scale(1.05) !important;
69
+ background-color: #c2185b !important;
70
+ }
71
+
72
+ /* Label styling */
73
+ label {
74
+ color: #333 !important;
75
+ font-weight: 500 !important;
76
+ margin-bottom: 8px !important;
77
  }
78
  """
79
 
80
  def analyze_with_loading(text, progress=gr.Progress()):
81
+ """
82
+ Synchronous wrapper for the async analyze_content function
83
+ """
84
  # Initialize progress
85
  progress(0, desc="Starting analysis...")
86
 
87
+ # Initial setup phase
88
  for i in range(30):
89
+ time.sleep(0.02) # Reduced sleep time
90
  progress((i + 1) / 100)
91
 
92
+ # Perform analysis
93
  progress(0.3, desc="Processing text...")
94
+ try:
95
+ # Run the async function in a sync context
96
+ loop = asyncio.get_event_loop()
97
+ result = loop.run_until_complete(analyze_content(text))
98
+ except Exception as e:
99
+ return f"Error during analysis: {str(e)}"
100
 
101
+ # Final processing
102
  for i in range(70, 100):
103
+ time.sleep(0.02) # Reduced sleep time
104
  progress((i + 1) / 100)
105
 
106
+ # Format the results
107
  triggers = result["detected_triggers"]
108
  if triggers == ["None"]:
109
+ return "No triggers detected in the content."
110
  else:
111
  trigger_list = "\n".join([f"• {trigger}" for trigger in triggers])
112
+ return f"Triggers Detected:\n{trigger_list}"
113
 
114
+ # Create the Gradio interface
115
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as iface:
116
+ # Title section
117
  gr.HTML("""
118
  <div class="treat-title">
119
  <h1>TREAT</h1>
 
125
  </div>
126
  """)
127
 
128
+ # Content input section
129
+ with gr.Row():
130
+ with gr.Column(elem_classes="content-area"):
131
+ input_text = gr.Textbox(
132
+ label="Content to Analyze",
133
+ placeholder="Paste your content here...",
134
+ lines=8
135
+ )
136
 
137
+ # Button section
138
+ with gr.Row(justify="center"):
139
+ analyze_btn = gr.Button(
140
+ "✨ Analyze Content",
141
+ variant="primary"
 
 
 
 
 
 
142
  )
143
 
144
+ # Results section
145
+ with gr.Row():
146
+ with gr.Column(elem_classes="results-area"):
147
+ output_text = gr.Textbox(
148
+ label="Analysis Results",
149
+ lines=5,
150
+ interactive=False
151
+ )
152
+
153
  # Set up the click event
154
  analyze_btn.click(
155
  fn=analyze_with_loading,
 
159
  )
160
 
161
  if __name__ == "__main__":
162
+ # Launch with custom configurations
163
+ iface.launch(
164
+ share=False,
165
+ debug=True,
166
+ show_error=True,
167
+ ssr=False # Disable SSR to prevent potential issues
168
+ )