Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,256 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
level=logging.INFO,
|
9 |
-
format="%(asctime)s [%(levelname)s] %(message)s",
|
10 |
-
handlers=[
|
11 |
-
logging.FileHandler("/home/pi5/horrorvidmaker/test_write_json.log"),
|
12 |
-
logging.StreamHandler()
|
13 |
-
]
|
14 |
-
)
|
15 |
-
logger = logging.getLogger(__name__)
|
16 |
|
17 |
-
#
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"theme": "horror",
|
29 |
-
"timestamp": datetime.now().isoformat()
|
30 |
-
}
|
31 |
-
]
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
|
|
48 |
if __name__ == "__main__":
|
49 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
|
8 |
+
# Initialize sentiment analysis pipeline (lightweight for CPU)
|
9 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Mock text-to-image function (CPU-friendly)
|
12 |
+
def generate_mock_image(text_prompt, width=200, height=200):
|
13 |
+
img_array = np.zeros((height, width, 3), dtype=np.uint8)
|
14 |
+
for i in range(height):
|
15 |
+
for j in range(width):
|
16 |
+
img_array[i, j] = [(i % 255), (j % 255), ((i + j) % 255)] # RGB gradient
|
17 |
+
img = Image.fromarray(img_array)
|
18 |
+
buffered = io.BytesIO()
|
19 |
+
img.save(buffered, format="PNG")
|
20 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
21 |
+
return f"data:image/png;base64,{img_str}"
|
22 |
|
23 |
+
# Sentiment analysis function
|
24 |
+
def analyze_sentiment(text):
|
25 |
+
if not text.strip():
|
26 |
+
return "Please enter some text."
|
27 |
+
result = sentiment_analyzer(text)[0]
|
28 |
+
label = result['label']
|
29 |
+
score = result['score']
|
30 |
+
return f"Sentiment: {label} (Confidence: {score:.2%})"
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Chatbot feedback function
|
33 |
+
def chatbot_response(user_feedback, chat_history):
|
34 |
+
if not user_feedback.strip():
|
35 |
+
return chat_history, "Please provide feedback."
|
36 |
+
chat_history.append((
|
37 |
+
f"**You**: {user_feedback}",
|
38 |
+
f"**Bot**: Thanks for your feedback! I understood: '{user_feedback}'."
|
39 |
+
))
|
40 |
+
return chat_history, ""
|
41 |
|
42 |
+
# Custom CSS for a dark grey, minimalist UI
|
43 |
+
custom_css = """
|
44 |
+
body, .gradio-container {
|
45 |
+
background: #2d2d2d !important;
|
46 |
+
color: #d4d4d4 !important;
|
47 |
+
font-family: 'Inter', -apple-system, sans-serif;
|
48 |
+
margin: 0;
|
49 |
+
padding: 20px;
|
50 |
+
}
|
51 |
+
.tab-nav button {
|
52 |
+
background: #3a3a3a !important;
|
53 |
+
color: #a3a3a3 !important;
|
54 |
+
border: none !important;
|
55 |
+
padding: 12px 20px !important;
|
56 |
+
border-radius: 8px 8px 0 0 !important;
|
57 |
+
transition: background 0.3s, color 0.3s;
|
58 |
+
}
|
59 |
+
.tab-nav button:hover, .tab-nav button[aria-selected="true"] {
|
60 |
+
background: #4a4a4a !important;
|
61 |
+
color: #e0e0e0 !important;
|
62 |
+
}
|
63 |
+
.block, .gr-panel {
|
64 |
+
background: #353535 !important;
|
65 |
+
border-radius: 10px !important;
|
66 |
+
padding: 20px !important;
|
67 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
|
68 |
+
margin-bottom: 20px;
|
69 |
+
}
|
70 |
+
input, textarea, .gr-textbox {
|
71 |
+
background: #2a2a2a !important;
|
72 |
+
color: #d4d4d4 !important;
|
73 |
+
border: 1px solid #4a4a4a !important;
|
74 |
+
border-radius: 8px !important;
|
75 |
+
padding: 12px !important;
|
76 |
+
transition: border-color 0.2s;
|
77 |
+
}
|
78 |
+
input:focus, textarea:focus {
|
79 |
+
border-color: #6b6b6b !important;
|
80 |
+
outline: none;
|
81 |
+
}
|
82 |
+
button {
|
83 |
+
background: #4a4a4a !important;
|
84 |
+
color: #e0e0e0 !important;
|
85 |
+
border: none !important;
|
86 |
+
border-radius: 8px !important;
|
87 |
+
padding: 12px 24px !important;
|
88 |
+
font-weight: 600;
|
89 |
+
transition: background 0.2s, transform 0.2s;
|
90 |
+
}
|
91 |
+
button:hover {
|
92 |
+
background: #5a5a5a !important;
|
93 |
+
transform: scale(1.03);
|
94 |
+
}
|
95 |
+
.gr-image img {
|
96 |
+
border-radius: 8px !important;
|
97 |
+
border: 2px solid #4a4a4a !important;
|
98 |
+
max-width: 100%;
|
99 |
+
}
|
100 |
+
.gr-chatbot .message {
|
101 |
+
border-radius: 8px !important;
|
102 |
+
padding: 12px !important;
|
103 |
+
margin: 8px 0 !important;
|
104 |
+
}
|
105 |
+
.gr-chatbot .message:nth-child(odd) {
|
106 |
+
background: #3a3a3a !important; /* User messages */
|
107 |
+
}
|
108 |
+
.gr-chatbot .message:nth-child(even) {
|
109 |
+
background: #2a2a2a !important; /* Bot messages */
|
110 |
+
}
|
111 |
+
h1, h2, h3 {
|
112 |
+
color: #b3b3b3 !important;
|
113 |
+
font-weight: 600;
|
114 |
+
}
|
115 |
+
.loading {
|
116 |
+
display: flex;
|
117 |
+
justify-content: center;
|
118 |
+
align-items: center;
|
119 |
+
height: 100px;
|
120 |
+
}
|
121 |
+
.loading::after {
|
122 |
+
content: '';
|
123 |
+
width: 24px;
|
124 |
+
height: 24px;
|
125 |
+
border: 4px solid #6b6b6b;
|
126 |
+
border-top: 4px solid transparent;
|
127 |
+
border-radius: 50%;
|
128 |
+
animation: spin 1s linear infinite;
|
129 |
+
}
|
130 |
+
@keyframes spin {
|
131 |
+
0% { transform: rotate(0deg); }
|
132 |
+
100% { transform: rotate(360deg); }
|
133 |
+
}
|
134 |
+
@media (max-width: 768px) {
|
135 |
+
.gradio-container {
|
136 |
+
padding: 10px;
|
137 |
+
}
|
138 |
+
.block {
|
139 |
+
padding: 15px !important;
|
140 |
+
}
|
141 |
+
button {
|
142 |
+
padding: 10px 20px !important;
|
143 |
+
}
|
144 |
+
.tab-nav button {
|
145 |
+
padding: 10px 15px !important;
|
146 |
+
font-size: 14px;
|
147 |
+
}
|
148 |
+
}
|
149 |
+
"""
|
150 |
|
151 |
+
# Main Gradio app with Tabs
|
152 |
+
with gr.Blocks(css=custom_css) as demo:
|
153 |
+
gr.Markdown(
|
154 |
+
"""
|
155 |
+
# 🛠️ Interactive AI Dashboard
|
156 |
+
Explore **Sentiment Analysis**, **Text-to-Image Generation**, and **Feedback Chatbot** in a sleek grey interface.
|
157 |
+
Built for Hugging Face Spaces (free tier, CPU-only).
|
158 |
+
"""
|
159 |
+
)
|
160 |
+
|
161 |
+
with gr.Tabs():
|
162 |
+
# Sentiment Analysis Tab
|
163 |
+
with gr.Tab("Sentiment Analysis"):
|
164 |
+
with gr.Row():
|
165 |
+
with gr.Column(scale=3):
|
166 |
+
gr.Markdown("### 📝 Analyze Text Sentiment")
|
167 |
+
sentiment_input = gr.Textbox(
|
168 |
+
label="Your Text",
|
169 |
+
placeholder="Enter text like 'This app is awesome!'",
|
170 |
+
lines=4,
|
171 |
+
show_label=False
|
172 |
+
)
|
173 |
+
sentiment_button = gr.Button("Analyze", variant="primary")
|
174 |
+
sentiment_output = gr.Textbox(
|
175 |
+
label="Result",
|
176 |
+
interactive=False,
|
177 |
+
placeholder="Sentiment result will appear here..."
|
178 |
+
)
|
179 |
+
with gr.Column(scale=2):
|
180 |
+
gr.Markdown("### Example Prompts")
|
181 |
+
gr.Examples(
|
182 |
+
examples=[
|
183 |
+
"I’m thrilled about this project!",
|
184 |
+
"Today feels a bit gloomy.",
|
185 |
+
"Programming is tough but rewarding!"
|
186 |
+
],
|
187 |
+
inputs=sentiment_input
|
188 |
+
)
|
189 |
+
sentiment_button.click(
|
190 |
+
fn=analyze_sentiment,
|
191 |
+
inputs=sentiment_input,
|
192 |
+
outputs=sentiment_output,
|
193 |
+
_js="() => { document.querySelector('.loading').style.display = 'block'; setTimeout(() => document.querySelector('.loading').style.display = 'none', 1000); }"
|
194 |
+
)
|
195 |
+
|
196 |
+
# Text-to-Image Tab
|
197 |
+
with gr.Tab("Text-to-Image"):
|
198 |
+
with gr.Row():
|
199 |
+
with gr.Column(scale=3):
|
200 |
+
gr.Markdown("### 🖼️ Generate Mock Images")
|
201 |
+
image_prompt = gr.Textbox(
|
202 |
+
label="Image Prompt",
|
203 |
+
placeholder="Describe an image, e.g., 'Abstract colorful pattern'",
|
204 |
+
lines=3,
|
205 |
+
show_label=False
|
206 |
+
)
|
207 |
+
image_button = gr.Button("Generate", variant="primary")
|
208 |
+
image_output = gr.Image(
|
209 |
+
label="Generated Image",
|
210 |
+
type="pil",
|
211 |
+
interactive=False
|
212 |
+
)
|
213 |
+
with gr.Column(scale=2):
|
214 |
+
gr.Markdown("### Info")
|
215 |
+
gr.Markdown(
|
216 |
+
"This mock generator creates gradient images to stay lightweight for the free tier."
|
217 |
+
)
|
218 |
+
image_button.click(
|
219 |
+
fn=generate_mock_image,
|
220 |
+
inputs=image_prompt,
|
221 |
+
outputs=image_output,
|
222 |
+
_js="() => { document.querySelector('.loading').style.display = 'block'; setTimeout(() => document.querySelector('.loading').style.display = 'none', 1000); }"
|
223 |
+
)
|
224 |
+
|
225 |
+
# Chatbot Tab
|
226 |
+
with gr.Tab("Feedback Chatbot"):
|
227 |
+
with gr.Row():
|
228 |
+
with gr.Column():
|
229 |
+
gr.Markdown("### 💬 Share Your Thoughts")
|
230 |
+
chatbot = gr.Chatbot(
|
231 |
+
label="Conversation",
|
232 |
+
bubble_full_width=False,
|
233 |
+
height=400
|
234 |
+
)
|
235 |
+
feedback_input = gr.Textbox(
|
236 |
+
label="Your Message",
|
237 |
+
placeholder="Type your feedback here...",
|
238 |
+
lines=2,
|
239 |
+
show_label=False
|
240 |
+
)
|
241 |
+
feedback_button = gr.Button("Send", variant="primary")
|
242 |
+
feedback_output = gr.Textbox(
|
243 |
+
label="Status",
|
244 |
+
interactive=False,
|
245 |
+
placeholder="Bot response status..."
|
246 |
+
)
|
247 |
+
feedback_button.click(
|
248 |
+
fn=chatbot_response,
|
249 |
+
inputs=[feedback_input, chatbot],
|
250 |
+
outputs=[chatbot, feedback_output],
|
251 |
+
_js="() => { document.querySelector('.loading').style.display = 'block'; setTimeout(() => document.querySelector('.loading').style.display = 'none', 500); }"
|
252 |
+
)
|
253 |
|
254 |
+
# Launch the app
|
255 |
if __name__ == "__main__":
|
256 |
+
demo.launch()
|