Spaces:
Running
Running
File size: 12,701 Bytes
ca2c89c |
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
from flask import Flask, render_template, request, jsonify, session
import os
import json
from datetime import datetime
# Import components
from components.preprocessing import preprocessing_handler
from components.tokenization import tokenization_handler
from components.pos_tagging import pos_tagging_handler
from components.named_entity import named_entity_handler
from components.sentiment import sentiment_handler
from components.summarization import summarization_handler
from components.topic_analysis import topic_analysis_handler
from components.question_answering import question_answering_handler
from components.text_generation import text_generation_handler
from components.translation import translation_handler
from components.classification import classification_handler
from components.vector_embeddings import vector_embeddings_handler
# Import utilities
from utils.model_loader_hf import download_nltk_resources, load_spacy, initialize_essential_models
from utils.helpers import text_statistics
app = Flask(__name__)
app.secret_key = 'your-secret-key-here' # Change this in production
# Sample texts
SAMPLE_TEXTS = {
"News Article": "The European Commission has fined Google €1.49 billion for abusive practices in online advertising. Google abused its market dominance by imposing restrictive clauses in contracts with third-party websites, preventing competitors from placing their search adverts on these websites.",
"Product Review": "I absolutely love this smartphone! The camera quality is outstanding and the battery life is impressive. The user interface is intuitive and the performance is smooth even when running multiple apps. However, I find the price a bit high compared to similar models on the market.",
"Scientific Text": "Climate change is the long-term alteration of temperature and typical weather patterns in a place. The cause of current climate change is largely human activity, like burning fossil fuels, which adds heat-trapping gases to Earth's atmosphere. The consequences of changing climate are already being felt worldwide.",
"Literary Text": "The old man was thin and gaunt with deep wrinkles in the back of his neck. The brown blotches of the benevolent skin cancer the sun brings from its reflection on the tropical sea were on his cheeks. The blotches ran well down the sides of his face and his hands had the deep-creased scars from handling heavy fish on the cords."
}
# Initialize essential models for HF Spaces
initialize_essential_models()
@app.route('/')
def index():
"""Main page with text input and analysis options"""
return render_template('index.html', sample_texts=SAMPLE_TEXTS)
@app.route('/api/text-stats', methods=['POST'])
def get_text_stats():
"""API endpoint to get text statistics"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
stats = text_statistics(text)
return jsonify(stats)
@app.route('/api/sample-text', methods=['POST'])
def get_sample_text():
"""API endpoint to get sample text"""
data = request.get_json()
sample_type = data.get('sample_type', 'Custom')
if sample_type == "Custom":
return jsonify({'text': ''})
else:
return jsonify({'text': SAMPLE_TEXTS.get(sample_type, '')})
# Text Processing Routes
@app.route('/preprocessing')
def preprocessing():
"""Text preprocessing page"""
return render_template('preprocessing.html')
@app.route('/api/preprocessing', methods=['POST'])
def api_preprocessing():
"""API endpoint for text preprocessing"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = preprocessing_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/tokenization')
def tokenization():
"""Tokenization page"""
return render_template('tokenization.html')
@app.route('/api/tokenization', methods=['POST'])
def api_tokenization():
"""API endpoint for tokenization"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = tokenization_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/pos-tagging')
def pos_tagging():
"""POS tagging page"""
return render_template('pos_tagging.html')
@app.route('/api/pos-tagging', methods=['POST'])
def api_pos_tagging():
"""API endpoint for POS tagging"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = pos_tagging_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/named-entity')
def named_entity():
"""Named entity recognition page"""
return render_template('named_entity.html')
@app.route('/api/named-entity', methods=['POST'])
def api_named_entity():
"""API endpoint for named entity recognition"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = named_entity_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
# Analysis Routes
@app.route('/sentiment')
def sentiment():
"""Sentiment analysis page"""
return render_template('sentiment.html')
@app.route('/api/sentiment', methods=['POST'])
def api_sentiment():
"""API endpoint for sentiment analysis"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = sentiment_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/summarization')
def summarization():
"""Text summarization page"""
return render_template('summarization.html')
@app.route('/api/summarization', methods=['POST'])
def api_summarization():
"""API endpoint for text summarization"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = summarization_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/topic-analysis')
def topic_analysis():
"""Topic analysis page"""
return render_template('topic_analysis.html')
@app.route('/api/topic-analysis', methods=['POST'])
def api_topic_analysis():
"""API endpoint for topic analysis"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = topic_analysis_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
# Advanced NLP Routes
@app.route('/question-answering')
def question_answering():
"""Question answering page"""
return render_template('question_answering.html')
@app.route('/api/question-answering', methods=['POST'])
def api_question_answering():
"""API endpoint for question answering"""
data = request.get_json(silent=True) or {}
# Accept from JSON, form, or query string
text = (
data.get('context')
or data.get('text')
or request.form.get('context')
or request.form.get('text')
or request.args.get('context')
or request.args.get('text')
or ''
)
question = (
data.get('question')
or request.form.get('question')
or request.args.get('question')
or ''
)
confidence_threshold = (
data.get('confidence_threshold')
or request.form.get('confidence_threshold')
or request.args.get('confidence_threshold')
or 0.5
)
try:
confidence_threshold = float(confidence_threshold)
except Exception:
confidence_threshold = 0.5
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = question_answering_handler(text, question, confidence_threshold=confidence_threshold)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/text-generation')
def text_generation():
"""Text generation page"""
return render_template('text_generation.html')
@app.route('/api/text-generation', methods=['POST'])
def api_text_generation():
"""API endpoint for text generation"""
data = request.get_json()
text = data.get('text', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = text_generation_handler(text)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/translation')
def translation():
"""Translation page"""
return render_template('translation.html')
@app.route('/api/translation', methods=['POST'])
def api_translation():
"""API endpoint for translation"""
data = request.get_json()
text = data.get('text', '')
target_language = data.get('target_language', 'en')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = translation_handler(text, target_language)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/classification')
def classification():
"""Classification page"""
return render_template('classification.html')
@app.route('/api/classification', methods=['POST'])
def api_classification():
"""API endpoint for classification"""
data = request.get_json()
text = data.get('text', '')
scenario = data.get('scenario', 'Sentiment')
multi_label = data.get('multi_label', False)
custom_labels = data.get('custom_labels', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = classification_handler(text, scenario, multi_label, custom_labels)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/vector-embeddings')
def vector_embeddings():
"""Vector embeddings page"""
return render_template('vector_embeddings.html')
@app.route('/api/vector-embeddings', methods=['POST'])
def api_vector_embeddings():
"""API endpoint for vector embeddings"""
data = request.get_json()
text = data.get('text', '')
query = data.get('query', '')
if not text:
return jsonify({'error': 'No text provided'}), 400
try:
result = vector_embeddings_handler(text, query)
return jsonify({'success': True, 'result': result})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/semantic-search', methods=['POST'])
def api_semantic_search():
"""API endpoint for semantic search"""
from components.vector_embeddings import perform_semantic_search
data = request.get_json()
context = data.get('context', '')
query = data.get('query', '')
if not context or not query:
return jsonify({'error': 'Both context and query are required'}), 400
try:
result = perform_semantic_search(context, query)
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
# For HF Spaces, run on port 7860
port = int(os.environ.get('PORT', 7860))
app.run(debug=False, host='0.0.0.0', port=port)
|