Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Initialize pipelines for generation and sentiment
|
| 5 |
+
haiku_generator = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 6 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 7 |
+
|
| 8 |
+
def generate_zen_output(text):
|
| 9 |
+
if not text.strip():
|
| 10 |
+
return "Please enter a thought.", "No sentiment detected."
|
| 11 |
+
|
| 12 |
+
# Generate Haiku using a prompt-based approach
|
| 13 |
+
haiku_prompt = f"Write a haiku about: {text}"
|
| 14 |
+
haiku_result = haiku_generator(haiku_prompt, max_length=50, num_beams=5)[0]['generated_text']
|
| 15 |
+
|
| 16 |
+
# Analyze Sentiment
|
| 17 |
+
sentiment_result = sentiment_analyzer(text)[0]
|
| 18 |
+
label = sentiment_result['label']
|
| 19 |
+
score = sentiment_result['score']
|
| 20 |
+
|
| 21 |
+
mood_indicator = f"{label} energy ({round(score * 100, 1)}% confidence)"
|
| 22 |
+
return haiku_result, mood_indicator
|
| 23 |
+
|
| 24 |
+
# UI Styling
|
| 25 |
+
css = ".gradio-container {background-color: #fcfaf2; font-family: 'Georgia', serif;} .main-title {text-align: center; color: #4a5568;}"
|
| 26 |
+
|
| 27 |
+
with gr.Blocks(css=css) as demo:
|
| 28 |
+
gr.Markdown("# 🌸 Zen Garden AI", elem_classes="main-title")
|
| 29 |
+
gr.Markdown("Transform your thoughts into haikus and visualize the emotional atmosphere of your words.")
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
with gr.Column():
|
| 33 |
+
input_box = gr.Textbox(placeholder="Describe a feeling, a place, or a moment...", label="Your Input", lines=3)
|
| 34 |
+
submit_btn = gr.Button("Seek Stillness", variant="primary")
|
| 35 |
+
|
| 36 |
+
with gr.Column():
|
| 37 |
+
output_haiku = gr.Textbox(label="Generated Haiku", interactive=False)
|
| 38 |
+
output_mood = gr.Label(label="Atmospheric Reading")
|
| 39 |
+
|
| 40 |
+
submit_btn.click(fn=generate_zen_output, inputs=input_box, outputs=[output_haiku, output_mood])
|
| 41 |
+
|
| 42 |
+
gr.Examples(
|
| 43 |
+
examples=["A quiet morning by the lake", "The chaotic energy of a big city", "Finding a forgotten letter in a book"],
|
| 44 |
+
inputs=input_box
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == '__main__':
|
| 48 |
+
demo.launch()
|