-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
113 lines (93 loc) · 3.51 KB
/
app.py
File metadata and controls
113 lines (93 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
from nlp_processor import NLPProcessor
from utils.logger import ProcessingLogger
import time
import json
app = Flask(__name__)
CORS(app)
# Initialize global instances
processor = None
logger = None
def get_processor():
global processor, logger
if processor is None:
processor = NLPProcessor()
logger = processor.logger
return processor, logger
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/process', methods=['POST'])
def process_text():
try:
processor, logger = get_processor()
data = request.get_json()
text = data.get('text', '').strip()
tasks = data.get('tasks', {})
thresholds = data.get('thresholds', {})
target_language = data.get('target_language', 'es')
if not text:
return jsonify({'error': 'No text provided'}), 400
# Validate text length
if len(text) < 10:
return jsonify({'error': 'Text too short (minimum 10 characters)'}), 400
# Update processor configuration
processor.update_thresholds(thresholds)
# Clear previous logs
logger.clear_logs()
# Process selected tasks
results = {}
processing_times = {}
for task_name, enabled in tasks.items():
if enabled:
start_time = time.time()
try:
if task_name == "Summarization":
result = processor.summarize(text)
elif task_name == "Text Generation":
result = processor.generate(text)
elif task_name == "Translation":
result = processor.translate(text, target_language)
elif task_name == "Sentiment Analysis":
result = processor.analyze_sentiment(text)
elif task_name == "Entity Extraction":
result = processor.extract_entities(text)
else:
continue
processing_times[task_name] = time.time() - start_time
results[task_name] = result
except Exception as task_error:
processing_times[task_name] = time.time() - start_time
results[task_name] = {
'success': False,
'error': str(task_error),
'confidence': 0.0,
'model_used': 'error'
}
return jsonify({
'success': True,
'results': results,
'processing_times': processing_times,
'logs': logger.get_logs()
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/logs')
def get_logs():
processor, logger = get_processor()
return jsonify(logger.get_logs())
@app.route('/api/config')
def get_config():
return jsonify({
'supported_languages': ['es', 'fr', 'de', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'hi', 'ar'],
'default_thresholds': {
'summarization': 0.7,
'generation': 0.6,
'translation': 0.8,
'sentiment': 0.75,
'entity': 0.7
}
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)