Deepakraj2006 commited on
Commit
e05207c
·
verified ·
1 Parent(s): b7e8fd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -15
app.py CHANGED
@@ -1,34 +1,55 @@
1
  import logging
2
  import os
3
- from flask import Flask, request, jsonify
4
  from flask_cors import CORS
5
- import worker
6
 
 
7
  app = Flask(__name__)
8
- CORS(app, resources={r"/*": {"origins": "*"}})
9
  app.logger.setLevel(logging.ERROR)
10
 
11
- @app.route('/')
 
12
  def index():
13
- return jsonify({"message": "Flask server is running!"})
14
 
 
15
  @app.route('/process-message', methods=['POST'])
16
  def process_message_route():
17
- user_message = request.json.get('userMessage', '')
18
- bot_response = worker.process_prompt(user_message)
19
- return jsonify({"botResponse": bot_response}), 200
20
 
 
 
 
 
 
 
 
 
21
  @app.route('/process-document', methods=['POST'])
22
  def process_document_route():
 
23
  if 'file' not in request.files:
24
- return jsonify({"botResponse": "No file uploaded."}), 400
 
 
 
 
 
 
 
 
25
 
26
- file = request.files['file']
27
- file_path = os.path.join("/tmp", file.filename) # Use temporary directory
28
- file.save(file_path)
29
 
30
- worker.process_document(file_path)
31
- return jsonify({"botResponse": "PDF processed. You can now ask questions."}), 200
 
 
 
32
 
 
33
  if __name__ == "__main__":
34
- app.run(debug=True, host='0.0.0.0', port=7860)
 
1
  import logging
2
  import os
3
+ from flask import Flask, render_template, request, jsonify
4
  from flask_cors import CORS
5
+ import worker # Import the worker module
6
 
7
+ # Initialize Flask app and CORS
8
  app = Flask(__name__)
9
+ cors = CORS(app, resources={r"/*": {"origins": "*"}})
10
  app.logger.setLevel(logging.ERROR)
11
 
12
+ # Define the route for the index page
13
+ @app.route('/', methods=['GET'])
14
  def index():
15
+ return render_template('index.html') # Render the index.html template
16
 
17
+ # Define the route for processing messages
18
  @app.route('/process-message', methods=['POST'])
19
  def process_message_route():
20
+ user_message = request.json['userMessage'] # Extract the user's message from the request
21
+ print('user_message', user_message)
 
22
 
23
+ bot_response = worker.process_prompt(user_message) # Process the user's message using the worker module
24
+
25
+ # Return the bot's response as JSON
26
+ return jsonify({
27
+ "botResponse": bot_response
28
+ }), 200
29
+
30
+ # Define the route for processing documents
31
  @app.route('/process-document', methods=['POST'])
32
  def process_document_route():
33
+ # Check if a file was uploaded
34
  if 'file' not in request.files:
35
+ return jsonify({
36
+ "botResponse": "It seems like the file was not uploaded correctly, can you try "
37
+ "again. If the problem persists, try using a different file"
38
+ }), 400
39
+
40
+ file = request.files['file'] # Extract the uploaded file from the request
41
+
42
+ file_path = file.filename # Define the path where the file will be saved
43
+ file.save(file_path) # Save the file
44
 
45
+ worker.process_document(file_path) # Process the document using the worker module
 
 
46
 
47
+ # Return a success message as JSON
48
+ return jsonify({
49
+ "botResponse": "Thank you for providing your PDF document. I have analyzed it, so now you can ask me any "
50
+ "questions regarding it!"
51
+ }), 200
52
 
53
+ # Run the Flask app
54
  if __name__ == "__main__":
55
+ app.run(debug=True, port=8000, host='0.0.0.0')