Athspi commited on
Commit
1deacc5
·
verified ·
1 Parent(s): f5c7eed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -4
app.py CHANGED
@@ -1,21 +1,38 @@
1
  # app.py - Flask Backend
2
- from flask import Flask, request, jsonify
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
5
  import os
6
  from flask_cors import CORS
 
 
7
 
8
  # Load environment variables
9
  load_dotenv()
10
 
11
  # Initialize Flask app
12
- app = Flask(__name__)
13
  CORS(app) # Enable CORS for all routes
14
 
15
  # Configure Gemini
16
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
17
  model = genai.GenerativeModel('gemini-2.5-flash')
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  @app.route('/chat', methods=['POST'])
20
  def chat():
21
  try:
@@ -28,8 +45,11 @@ def chat():
28
  # Generate response using Gemini
29
  response = model.generate_content(user_message)
30
 
 
 
 
31
  return jsonify({
32
- "response": response.text
33
  })
34
 
35
  except Exception as e:
@@ -37,7 +57,11 @@ def chat():
37
 
38
  @app.route('/')
39
  def serve_index():
40
- return app.send_static_file('index.html')
 
 
 
 
41
 
42
  if __name__ == '__main__':
43
  app.run(host="0.0.0.0", port=7860)
 
1
  # app.py - Flask Backend
2
+ from flask import Flask, request, jsonify, send_from_directory
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
5
  import os
6
  from flask_cors import CORS
7
+ import markdown2
8
+ import re
9
 
10
  # Load environment variables
11
  load_dotenv()
12
 
13
  # Initialize Flask app
14
+ app = Flask(__name__, static_folder='static')
15
  CORS(app) # Enable CORS for all routes
16
 
17
  # Configure Gemini
18
  genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
19
  model = genai.GenerativeModel('gemini-2.5-flash')
20
 
21
+ def convert_markdown_to_html(text):
22
+ # Convert markdown to HTML
23
+ html = markdown2.markdown(text)
24
+
25
+ # Add custom styling to code blocks
26
+ html = html.replace('<code>', '<code class="code-block">')
27
+
28
+ # Convert **bold** to <strong> for better visibility
29
+ html = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html)
30
+
31
+ # Convert *italic* to <em>
32
+ html = re.sub(r'\*(.*?)\*', r'<em>\1</em>', html)
33
+
34
+ return html
35
+
36
  @app.route('/chat', methods=['POST'])
37
  def chat():
38
  try:
 
45
  # Generate response using Gemini
46
  response = model.generate_content(user_message)
47
 
48
+ # Convert markdown to HTML with styling
49
+ formatted_response = convert_markdown_to_html(response.text)
50
+
51
  return jsonify({
52
+ "response": formatted_response
53
  })
54
 
55
  except Exception as e:
 
57
 
58
  @app.route('/')
59
  def serve_index():
60
+ return send_from_directory('static', 'index.html')
61
+
62
+ @app.route('/<path:path>')
63
+ def serve_static(path):
64
+ return send_from_directory('static', path)
65
 
66
  if __name__ == '__main__':
67
  app.run(host="0.0.0.0", port=7860)