Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app2.py 2.5 KB

You have to be logged in to leave a comment. Sign In
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
  1. from flask import Flask, render_template, request, redirect, flash, send_from_directory, session, url_for
  2. from werkzeug.utils import secure_filename
  3. import os
  4. from PIL import Image
  5. import logging
  6. app = Flask(__name__)
  7. # Configuration
  8. UPLOAD_FOLDER = 'uploads/'
  9. RESULTS_FOLDER = 'results/'
  10. app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  11. app.config['RESULTS_FOLDER'] = RESULTS_FOLDER
  12. app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif','mp4'}
  13. app.config['SECRET_KEY'] = 'your_very_secret_key'
  14. # Ensure directories exist
  15. os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
  16. os.makedirs(app.config['RESULTS_FOLDER'], exist_ok=True)
  17. # Configure logging
  18. logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  19. def allowed_file(filename):
  20. return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
  21. @app.route('/', methods=['GET', 'POST'])
  22. def home():
  23. if request.method == 'POST':
  24. # Initialize empty session data
  25. session['uploaded_file_url'] = None
  26. session['result_image_url'] = None
  27. file = request.files.get('file')
  28. if not file or file.filename == '':
  29. flash('No selected file')
  30. return redirect(request.url)
  31. if file and allowed_file(file.filename):
  32. filename = secure_filename(file.filename)
  33. filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  34. file.save(filepath)
  35. # Save the uploaded image URL to session
  36. session['uploaded_file_url'] = url_for('uploaded_file', filename=filename)
  37. # Redirect to the results endpoint to display uploaded image
  38. return redirect(url_for('results'))
  39. # If there's no POST request or after the images are cleared, just render the home page
  40. return render_template('index1.html')
  41. @app.route('/results')
  42. def results():
  43. # Retrieve the image URLs from session
  44. uploaded_file_url = session.get('uploaded_file_url')
  45. # Render the template with the uploaded image URL
  46. return render_template('index1.html', uploaded_file_url=uploaded_file_url)
  47. @app.route('/uploads/<filename>')
  48. def uploaded_file(filename):
  49. # Check if the file exists in the UPLOAD_FOLDER
  50. if os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], filename)):
  51. return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
  52. else:
  53. return "File not found", 404
  54. if __name__ == '__main__':
  55. app.run(host='0.0.0.0', debug=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...