ZENLLC commited on
Commit
74fcd5b
·
verified ·
1 Parent(s): 8140350

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -30
app.py CHANGED
@@ -1,48 +1,88 @@
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()
 
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()