-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
59 lines (49 loc) · 2.18 KB
/
app.py
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
import csv
import subprocess
from flask import Flask, jsonify, request
# from flask_restful import Api, Resource, reqparse, abort, fields, marshal_with
app = Flask(__name__)
# api = Api(app)
@app.route('/docs_info/<int:doc_id>', methods=['GET'])
def get_sim_docs(doc_id):
# Open CSV file
with open('similarities.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
# Check if the row corresponds to the requested doc_id
if int(row['Doc ID']) == doc_id:
# If found, return the similarity percentage as JSON response
sim1 = float(row['Sim1'])*100
sim2 = float(row['Sim2'])*100
sim3 = float(row['Sim3'])*100
sim4 = float(row['Sim4'])*100
return jsonify({ 'docs': [ int(row['D1']), int(row['D2']), int(row['D3']), int(row['D4']) ],
'sim': [sim1, sim2, sim3, sim4] })
# If not found, return a 404 error
return jsonify({'error': 'doc not found'}), 404
@app.route('/docs_upload', methods=['POST'])
def add_ipfs_link():
# Parse the request JSON data
req_data = request.get_json()
doc_id = req_data['doc_id']
ipfs_link = req_data['ipfs_link']
# Check if the doc_id already exists in the CSV file
with open('ipfs_links.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
if row[1] == str(doc_id):
return jsonify({'message': f"Document with ID {doc_id} already exists."}), 400
# Append the IPFS link to the CSV file
with open('ipfs_links.csv', mode='a', newline='') as file:
writer = csv.writer(file)
# Find the serial no for the new link
with open('ipfs_links.csv', 'r') as f:
reader = csv.reader(f)
serial_no = sum(1 for row in reader)
writer.writerow([serial_no+1, doc_id, ipfs_link])
# Run another Python script in the same directory
subprocess.run(['python', 'similarity.py'])
return jsonify({'message': 'Document added successfully.'}), 200
# api.add_resource(TestClass, "/next")
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')