Spaces:
Runtime error
Runtime error
SyntaxSpins commited on
Commit Β·
cdbc704
1
Parent(s): cd51b8c
Refactor moment
Browse files- app.py +19 -30
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -4,21 +4,16 @@ import os
|
|
| 4 |
import logging
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
# Configure logging
|
| 8 |
logging.basicConfig(
|
| 9 |
level=logging.DEBUG,
|
| 10 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 11 |
handlers=[logging.StreamHandler()]
|
| 12 |
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
genai_logger = logging.getLogger('google.generativeai')
|
| 16 |
-
genai_logger.setLevel(logging.DEBUG)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
logger = logging.getLogger(__name__)
|
| 20 |
-
|
| 21 |
-
app = Flask(__name__)
|
| 22 |
|
| 23 |
# Initialize Gemini Model globally
|
| 24 |
model = None
|
|
@@ -43,7 +38,7 @@ def initialize_model():
|
|
| 43 |
|
| 44 |
def clean_markdown(text):
|
| 45 |
"""Clean and format markdown text for compatibility"""
|
| 46 |
-
text = text.replace('`
|
| 47 |
escaped = []
|
| 48 |
code_block = False
|
| 49 |
|
|
@@ -69,17 +64,22 @@ def index():
|
|
| 69 |
'backend': 'Google Gemini'
|
| 70 |
})
|
| 71 |
|
| 72 |
-
@app.route('/ask', methods=['POST'])
|
| 73 |
def ask():
|
| 74 |
-
"""Handle chat requests"""
|
| 75 |
if not model:
|
| 76 |
-
# Try to initialize model if it's not loaded
|
| 77 |
if not initialize_model():
|
| 78 |
return jsonify({'error': 'AI service not configured'}), 503
|
| 79 |
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
return jsonify({'error': 'No message provided'}), 400
|
| 84 |
|
| 85 |
# Personality prompt with Markdown
|
|
@@ -90,7 +90,7 @@ def ask():
|
|
| 90 |
- Use `code` for technical terms
|
| 91 |
- Keep it short but stylish! π
"""
|
| 92 |
|
| 93 |
-
prompt = f"{personality}\n\nUser: {
|
| 94 |
|
| 95 |
logger.debug(f"Input prompt: {prompt}")
|
| 96 |
|
|
@@ -107,29 +107,18 @@ def ask():
|
|
| 107 |
|
| 108 |
logger.debug(f"Markdown formatted response: {markdown_response}")
|
| 109 |
|
| 110 |
-
|
| 111 |
-
if not any(c in markdown_response for c in ['π', 'π', 'π', 'π€', 'π']):
|
| 112 |
-
markdown_response += " π"
|
| 113 |
-
|
| 114 |
-
# Ensure the response isn't too long
|
| 115 |
-
if len(markdown_response) > 500:
|
| 116 |
-
markdown_response = markdown_response[:497] + "..."
|
| 117 |
-
|
| 118 |
-
return jsonify({
|
| 119 |
-
'response': markdown_response
|
| 120 |
-
})
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
logger.error(f"Error generating response: {e}", exc_info=True)
|
| 124 |
return jsonify({'error': 'Failed to generate response'}), 500
|
| 125 |
|
| 126 |
-
if
|
| 127 |
logger.info("Starting AI Service...")
|
| 128 |
try:
|
| 129 |
-
# Initialize model when the app starts
|
| 130 |
if not initialize_model():
|
| 131 |
logger.error("Model initialization failed, exiting.")
|
| 132 |
exit(1)
|
| 133 |
-
app.run(host='0.0.0.0', port=7860)
|
| 134 |
except Exception as e:
|
| 135 |
-
logger.error(f"Failed to start AI Service: {e}", exc_info=True)
|
|
|
|
| 4 |
import logging
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# Configure logging
|
| 8 |
logging.basicConfig(
|
| 9 |
level=logging.DEBUG,
|
| 10 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 11 |
handlers=[logging.StreamHandler()]
|
| 12 |
)
|
| 13 |
|
| 14 |
+
logger = logging.getLogger(name)
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
app = Flask(name)
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Initialize Gemini Model globally
|
| 19 |
model = None
|
|
|
|
| 38 |
|
| 39 |
def clean_markdown(text):
|
| 40 |
"""Clean and format markdown text for compatibility"""
|
| 41 |
+
text = text.replace('`', '`') # Clean code block formatting
|
| 42 |
escaped = []
|
| 43 |
code_block = False
|
| 44 |
|
|
|
|
| 64 |
'backend': 'Google Gemini'
|
| 65 |
})
|
| 66 |
|
| 67 |
+
@app.route('/ask', methods=['GET', 'POST'])
|
| 68 |
def ask():
|
| 69 |
+
"""Handle chat requests (GET and POST)"""
|
| 70 |
if not model:
|
|
|
|
| 71 |
if not initialize_model():
|
| 72 |
return jsonify({'error': 'AI service not configured'}), 503
|
| 73 |
|
| 74 |
try:
|
| 75 |
+
# Support both POST (JSON) and GET (Query Parameter)
|
| 76 |
+
if request.method == 'POST':
|
| 77 |
+
data = request.get_json()
|
| 78 |
+
message = data.get('message') if data else None
|
| 79 |
+
else: # GET request
|
| 80 |
+
message = request.args.get('message')
|
| 81 |
+
|
| 82 |
+
if not message:
|
| 83 |
return jsonify({'error': 'No message provided'}), 400
|
| 84 |
|
| 85 |
# Personality prompt with Markdown
|
|
|
|
| 90 |
- Use `code` for technical terms
|
| 91 |
- Keep it short but stylish! π
"""
|
| 92 |
|
| 93 |
+
prompt = f"{personality}\n\nUser: {message}\nMaha:"
|
| 94 |
|
| 95 |
logger.debug(f"Input prompt: {prompt}")
|
| 96 |
|
|
|
|
| 107 |
|
| 108 |
logger.debug(f"Markdown formatted response: {markdown_response}")
|
| 109 |
|
| 110 |
+
return jsonify({'response': markdown_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
logger.error(f"Error generating response: {e}", exc_info=True)
|
| 114 |
return jsonify({'error': 'Failed to generate response'}), 500
|
| 115 |
|
| 116 |
+
if name == 'main':
|
| 117 |
logger.info("Starting AI Service...")
|
| 118 |
try:
|
|
|
|
| 119 |
if not initialize_model():
|
| 120 |
logger.error("Model initialization failed, exiting.")
|
| 121 |
exit(1)
|
| 122 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|
| 123 |
except Exception as e:
|
| 124 |
+
logger.error(f"Failed to start AI Service: {e}", exc_info=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,5 @@
|
|
| 1 |
flask==3.1.0
|
| 2 |
google-generativeai==0.8.4
|
|
|
|
|
|
|
| 3 |
python-dotenv==1.0.1
|
|
|
|
| 1 |
flask==3.1.0
|
| 2 |
google-generativeai==0.8.4
|
| 3 |
+
gunicorn==21.2.0
|
| 4 |
+
requests==2.31.0
|
| 5 |
python-dotenv==1.0.1
|