Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from image_api import generate_krishna_image, generate_comic_strip
|
|
7 |
from countdown import get_countdown
|
8 |
from messages import daily_blessings
|
9 |
import logging
|
|
|
10 |
|
11 |
# Configure logging
|
12 |
logging.basicConfig(level=logging.INFO)
|
@@ -23,7 +24,7 @@ except ImportError as e:
|
|
23 |
|
24 |
# Load environment variables
|
25 |
load_dotenv()
|
26 |
-
|
27 |
app = Flask(__name__)
|
28 |
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
|
29 |
|
@@ -42,7 +43,7 @@ DAILY_MESSAGES = {
|
|
42 |
FESTIVAL_MESSAGES = {
|
43 |
(1, 26): {"message": "Happy Republic Day! Let Dharma guide our nation.", "verse": "Bhagavad Gita 4.7", "special": True},
|
44 |
(8, 19): {"message": "Happy Krishna Janmashtami! Celebrate the divine birth.", "verse": "Bhagavad Gita 4.9", "special": True},
|
45 |
-
(4, 19): {"message": "Happy Birthday, Manavi! I’ve brought Vrindavan’s magic to celebrate with you!", "verse": "Bhagavad Gita 2.22", "special": True}
|
46 |
}
|
47 |
|
48 |
def get_daily_message_and_blessing():
|
@@ -72,6 +73,46 @@ def get_daily_message_and_blessing():
|
|
72 |
"blessing": daily_blessing
|
73 |
}
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
@app.route('/')
|
76 |
def home():
|
77 |
"""Render the home page with daily message and blessing"""
|
@@ -98,7 +139,13 @@ def chat():
|
|
98 |
|
99 |
@app.route('/message')
|
100 |
def message():
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
@app.route('/image', methods=['POST'])
|
104 |
def image():
|
@@ -119,5 +166,5 @@ def countdown():
|
|
119 |
return jsonify({'days': days_left})
|
120 |
|
121 |
if __name__ == '__main__':
|
122 |
-
port = int(os.environ.get('PORT', 5000))
|
123 |
app.run(host='0.0.0.0', port=port, debug=False)
|
|
|
7 |
from countdown import get_countdown
|
8 |
from messages import daily_blessings
|
9 |
import logging
|
10 |
+
import requests
|
11 |
|
12 |
# Configure logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
|
|
24 |
|
25 |
# Load environment variables
|
26 |
load_dotenv()
|
27 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
28 |
app = Flask(__name__)
|
29 |
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
|
30 |
|
|
|
43 |
FESTIVAL_MESSAGES = {
|
44 |
(1, 26): {"message": "Happy Republic Day! Let Dharma guide our nation.", "verse": "Bhagavad Gita 4.7", "special": True},
|
45 |
(8, 19): {"message": "Happy Krishna Janmashtami! Celebrate the divine birth.", "verse": "Bhagavad Gita 4.9", "special": True},
|
46 |
+
(4, 19): {"message": "Happy Birthday, Manavi! I’ve brought Vrindavan’s magic to celebrate with you!", "verse": "Bhagavad Gita 2.22", "special": True}
|
47 |
}
|
48 |
|
49 |
def get_daily_message_and_blessing():
|
|
|
73 |
"blessing": daily_blessing
|
74 |
}
|
75 |
|
76 |
+
def generate_birthday_message():
|
77 |
+
"""Generate a unique birthday message for Manavi using an AI model."""
|
78 |
+
headers = {
|
79 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}",
|
80 |
+
"Content-Type": "application/json"
|
81 |
+
}
|
82 |
+
prompt = (
|
83 |
+
"You are Little Krishna, a playful and loving cowherd from Vrindavan, speaking to Manavi on her birthday, April 19, 2025. "
|
84 |
+
"Ayush has created this chatbot as a surprise for her, and you are his wingman. "
|
85 |
+
"Write a short, heartfelt birthday message (1-2 sentences) for Manavi, filled with Vrindavan imagery and a playful tone. "
|
86 |
+
"Example: 'Hare Manavi! On your special day, I’ve brought the sweetest butter from Vrindavan and a flute melody to make your heart dance—happy birthday, my dear gopi!'"
|
87 |
+
)
|
88 |
+
payload = {
|
89 |
+
"inputs": prompt,
|
90 |
+
"parameters": {
|
91 |
+
"max_length": 60,
|
92 |
+
"temperature": 0.9,
|
93 |
+
"top_p": 0.9,
|
94 |
+
"top_k": 50
|
95 |
+
}
|
96 |
+
}
|
97 |
+
try:
|
98 |
+
response = requests.post(
|
99 |
+
"https://api-inference.huggingface.co/models/bigscience/bloom-560m",
|
100 |
+
headers=headers,
|
101 |
+
json=payload
|
102 |
+
)
|
103 |
+
if response.status_code == 200:
|
104 |
+
result = response.json()
|
105 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
106 |
+
return result[0]["generated_text"].strip()
|
107 |
+
elif isinstance(result, dict) and "generated_text" in result:
|
108 |
+
return result["generated_text"].strip()
|
109 |
+
elif isinstance(result, str):
|
110 |
+
return result.strip()
|
111 |
+
return "Hare Manavi! On your special day, I’ve brought the sweetest butter from Vrindavan and a flute melody to make your heart dance—happy birthday, my dear gopi!"
|
112 |
+
except Exception as e:
|
113 |
+
logger.error(f"Error generating birthday message: {str(e)}")
|
114 |
+
return "Hare Manavi! On your special day, I’ve brought the sweetest butter from Vrindavan and a flute melody to make your heart dance—happy birthday, my dear gopi!"
|
115 |
+
|
116 |
@app.route('/')
|
117 |
def home():
|
118 |
"""Render the home page with daily message and blessing"""
|
|
|
139 |
|
140 |
@app.route('/message')
|
141 |
def message():
|
142 |
+
birthday_message = generate_birthday_message()
|
143 |
+
# Generate a birthday image for Manavi
|
144 |
+
image_prompt = "Little Krishna celebrating Manavi’s birthday in Vrindavan, with peacocks, butter pots, and a festive atmosphere, cartoon style"
|
145 |
+
birthday_image_url = generate_krishna_image(image_prompt)
|
146 |
+
if not birthday_image_url:
|
147 |
+
birthday_image_url = "/static/assets/krishna.png" # Fallback image
|
148 |
+
return render_template('message.html', birthday_message=birthday_message, birthday_image_url=birthday_image_url)
|
149 |
|
150 |
@app.route('/image', methods=['POST'])
|
151 |
def image():
|
|
|
166 |
return jsonify({'days': days_left})
|
167 |
|
168 |
if __name__ == '__main__':
|
169 |
+
port = int(os.environ.get('PORT', 5000))
|
170 |
app.run(host='0.0.0.0', port=port, debug=False)
|