Update app/dip.py
Browse files- app/dip.py +44 -0
app/dip.py
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import ollama
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
# Initialize Flask app
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Configure logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
|
| 11 |
+
# Define Ollama model name (assuming you have it installed locally)
|
| 12 |
+
OLLAMA_MODEL_NAME = "llama3.2"
|
| 13 |
+
|
| 14 |
+
@app.route('/detect-depression', methods=['POST'])
|
| 15 |
+
def detect_depression():
|
| 16 |
+
try:
|
| 17 |
+
# Get input text from POST request
|
| 18 |
+
data = request.get_json()
|
| 19 |
+
|
| 20 |
+
if 'text' not in data or not data['text'].strip():
|
| 21 |
+
return jsonify({'error': 'No text provided'}), 400
|
| 22 |
+
|
| 23 |
+
text = data['text']
|
| 24 |
+
app.logger.info(f"Processing text: {text[:50]}...")
|
| 25 |
+
|
| 26 |
+
# Generate response using Ollama model
|
| 27 |
+
prompt = f"Analyze the following text for signs of depression and classify the severity as ZERO, MILD, INTERMEDIATE, or SEVERE: {text}"
|
| 28 |
+
response = ollama.chat(model=OLLAMA_MODEL_NAME, messages=[{"role": "user", "content": prompt}])
|
| 29 |
+
|
| 30 |
+
# Extract relevant information from the response
|
| 31 |
+
result_text = response['message']['content'] if 'message' in response else "No response received"
|
| 32 |
+
|
| 33 |
+
# Ensure response contains only the depression level
|
| 34 |
+
levels = ["ZERO", "MILD", "INTERMEDIATE", "SEVERE"]
|
| 35 |
+
depression_level = next((level for level in levels if level in result_text.upper()), "UNKNOWN")
|
| 36 |
+
|
| 37 |
+
return jsonify({'depression_level': depression_level})
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
app.logger.error(f"Error processing request: {e}")
|
| 41 |
+
return jsonify({'error': str(e)}), 500
|
| 42 |
+
|
| 43 |
+
if __name__ == '__main__':
|
| 44 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|