pmirkovic commited on
Commit
329cb40
·
verified ·
1 Parent(s): 980b240
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py - NEW version to serve HTML and API
2
+ from flask import Flask, request, jsonify, send_from_directory, render_template
3
+ from transformers import pipeline
4
+ import os
5
+
6
+ # Configure Flask: 'templates' for HTML files, 'static' for CSS/JS (if external)
7
+ app = Flask(__name__, template_folder='templates', static_folder='static')
8
+
9
+ # --- Initialize the AI Model (t5-small) ---
10
+ # This will download the model the first time your Space starts.
11
+ try:
12
+ # device=-1 forces CPU, necessary for free tier and mobile environments.
13
+ generator = pipeline("text2text-generation", model="t5-small", device=-1)
14
+ print("AI Model 't5-small' loaded successfully!")
15
+ except Exception as e:
16
+ print(f"Error loading AI model: {e}")
17
+ generator = None # Model failed to load
18
+
19
+ # --- Route to Serve Your Main Website (index.html) ---
20
+ @app.route('/')
21
+ def serve_index():
22
+ # When someone visits your Space's main URL, this sends them your index.html
23
+ return render_template('index.html')
24
+
25
+ # --- API Endpoint for AI Rewriting ---
26
+ @app.route('/rewrite', methods=['POST'])
27
+ def rewrite_text():
28
+ # Check if the AI model is ready
29
+ if generator is None:
30
+ return jsonify({"error": "AI model failed to load or is not ready."}), 503
31
+
32
+ # Get the message and tone from your website's request
33
+ data = request.get_json()
34
+ message = data.get('message', '')
35
+ tone = data.get('tone', 'kind') # Default to 'kind'
36
+
37
+ # Basic checks for valid input
38
+ if not message:
39
+ return jsonify({"error": "Message content is required."}), 400
40
+ if len(message) < 5:
41
+ return jsonify({"error": "Please provide a longer message for better AI rewriting."}), 400
42
+
43
+ # Craft the instruction for the AI
44
+ prompt = f"Rewrite the following message in a {tone} tone, maintaining its original meaning but adjusting the emotional nuance: '{message}'"
45
+
46
+ try:
47
+ # Generate rewritten text using the AI model
48
+ # max_length controls output length; num_beams improves quality but can be slower.
49
+ result = generator(prompt, max_length=150, min_length=20, num_beams=5, early_stopping=True)
50
+ rewritten_text = result[0]['generated_text']
51
+
52
+ # Send the rewritten text back to your website
53
+ return jsonify({"rewrittenText": rewritten_text})
54
+
55
+ except Exception as e:
56
+ print(f"Error during AI inference: {e}")
57
+ return jsonify({"error": f"An error occurred during AI rewriting: {str(e)}"}), 500
58
+
59
+ # --- Main Part of the Flask Application ---
60
+ if __name__ == '__main__':
61
+ # Hugging Face Spaces expects your app to run on port 7860
62
+ app.run(host='0.0.0.0', port=7860)
63
+