Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,88 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# Initialize
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
if
|
| 10 |
-
return "
|
| 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 |
-
#
|
| 17 |
-
|
| 18 |
-
label = sentiment_result['label']
|
| 19 |
-
score = sentiment_result['score']
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
gr.Markdown("
|
|
|
|
|
|
|
| 30 |
|
| 31 |
with gr.Row():
|
| 32 |
with gr.Column():
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
with gr.Column():
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
gr.Examples(
|
| 43 |
-
examples=["
|
| 44 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
-
if __name__ ==
|
| 48 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Initialize the pipeline with a small, efficient instruction-tuned model
|
| 6 |
+
# This model is specifically chosen for its small footprint and CPU compatibility
|
| 7 |
+
try:
|
| 8 |
+
generator = pipeline(
|
| 9 |
+
"text-generation",
|
| 10 |
+
model="MBZUAI/LaMini-GPT-124M",
|
| 11 |
+
device="cpu"
|
| 12 |
+
)
|
| 13 |
+
except Exception as e:
|
| 14 |
+
generator = None
|
| 15 |
|
| 16 |
+
def generate_meditation(mood, focus_topic):
|
| 17 |
+
if generator is None:
|
| 18 |
+
return "Model is still loading, please try again in a moment."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Constructing a structured prompt for the instruction-tuned model
|
| 21 |
+
prompt = f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\nWrite a calming, 3-sentence meditation script for someone feeling {mood}. The meditation should focus on the theme of {focus_topic}. Use soothing language.\n\n### Response:"
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
result = generator(
|
| 25 |
+
prompt,
|
| 26 |
+
max_new_tokens=150,
|
| 27 |
+
num_return_sequences=1,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
do_sample=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
full_text = result[0]['generated_text']
|
| 33 |
+
# Extract the text after the response marker
|
| 34 |
+
if "### Response:" in full_text:
|
| 35 |
+
response = full_text.split("### Response:")[1].strip()
|
| 36 |
+
else:
|
| 37 |
+
response = full_text
|
| 38 |
+
|
| 39 |
+
return response
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"An error occurred: {str(e)}"
|
| 42 |
|
| 43 |
+
# Custom CSS for a peaceful aesthetic
|
| 44 |
+
custom_css = """
|
| 45 |
+
.gradio-container {background-color: #f0f4f8}
|
| 46 |
+
#title-text {text-align: center; color: #2d5a27}
|
| 47 |
+
"""
|
| 48 |
|
| 49 |
+
# Build the Gradio Interface
|
| 50 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
|
| 51 |
+
gr.Markdown("# 🌿 AI Zen Garden", elem_id="title-text")
|
| 52 |
+
gr.Markdown("**Daily AI Challenge - 4/2/2026**")
|
| 53 |
+
gr.Markdown("Take a deep breath. Tell the AI how you feel, and let it guide you through a moment of peace.")
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
with gr.Column():
|
| 57 |
+
mood_input = gr.Dropdown(
|
| 58 |
+
choices=["Anxious", "Tired", "Restless", "Sad", "Overwhelmed", "Neutral"],
|
| 59 |
+
label="Current Mood",
|
| 60 |
+
value="Anxious"
|
| 61 |
+
)
|
| 62 |
+
focus_input = gr.Textbox(
|
| 63 |
+
label="Focus Topic",
|
| 64 |
+
placeholder="e.g., Summer Rain, Forest Silence, Soft Clouds...",
|
| 65 |
+
value="Mountain Air"
|
| 66 |
+
)
|
| 67 |
+
submit_btn = gr.Button("Generate Meditation", variant="primary")
|
| 68 |
|
| 69 |
with gr.Column():
|
| 70 |
+
output_text = gr.Textbox(
|
| 71 |
+
label="Your Personalized Script",
|
| 72 |
+
lines=8,
|
| 73 |
+
interactive=False
|
| 74 |
+
)
|
| 75 |
|
| 76 |
gr.Examples(
|
| 77 |
+
examples=[["Overwhelmed", "Ocean Waves"], ["Tired", "Warm Sunlight"]],
|
| 78 |
+
inputs=[mood_input, focus_input]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
submit_btn.click(
|
| 82 |
+
fn=generate_meditation,
|
| 83 |
+
inputs=[mood_input, focus_input],
|
| 84 |
+
outputs=output_text
|
| 85 |
)
|
| 86 |
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
demo.launch()
|