Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from backend.chatbot import get_krishna_response
|
5 |
+
from backend.image_api import generate_krishna_image, generate_comic_strip
|
6 |
+
from backend.countdown import get_countdown
|
7 |
+
try:
|
8 |
+
from backend.firebase_config import save_chat_message, get_chat_history
|
9 |
+
firebase_enabled = True
|
10 |
+
except ImportError:
|
11 |
+
firebase_enabled = False
|
12 |
+
|
13 |
+
# Load environment variables (Hugging Face Space secrets)
|
14 |
+
load_dotenv()
|
15 |
+
app = Flask(__name__)
|
16 |
+
|
17 |
+
@app.route('/')
|
18 |
+
def home():
|
19 |
+
return render_template('home.html')
|
20 |
+
|
21 |
+
@app.route('/chat', methods=['GET', 'POST'])
|
22 |
+
def chat():
|
23 |
+
if request.method == 'POST':
|
24 |
+
user_input = request.form['message']
|
25 |
+
reply = get_krishna_response(user_input)
|
26 |
+
if firebase_enabled:
|
27 |
+
save_chat_message(user_input, reply)
|
28 |
+
return jsonify({'reply': reply})
|
29 |
+
chat_history = get_chat_history() if firebase_enabled else []
|
30 |
+
return render_template('chat.html', chat_history=chat_history)
|
31 |
+
|
32 |
+
@app.route('/message')
|
33 |
+
def message():
|
34 |
+
return render_template('message.html')
|
35 |
+
|
36 |
+
@app.route('/image', methods=['POST'])
|
37 |
+
def image():
|
38 |
+
prompt = request.json['prompt']
|
39 |
+
image_url = generate_krishna_image(prompt)
|
40 |
+
if image_url:
|
41 |
+
return jsonify({'image_url': image_url})
|
42 |
+
return jsonify({'error': 'Failed to generate image'}), 500
|
43 |
+
|
44 |
+
@app.route('/comic', methods=['GET'])
|
45 |
+
def comic():
|
46 |
+
comic_images = generate_comic_strip()
|
47 |
+
return jsonify({'comic_images': comic_images})
|
48 |
+
|
49 |
+
@app.route('/countdown')
|
50 |
+
def countdown():
|
51 |
+
days_left = get_countdown()
|
52 |
+
return jsonify({'days': days_left})
|
53 |
+
|
54 |
+
if __name__ == '__main__':
|
55 |
+
app.run(host='0.0.0.0', port=5000)
|