dkdaniz commited on
Commit
784bcec
1 Parent(s): 418d9ef

Create frontend.py

Browse files
Files changed (1) hide show
  1. frontend.py +72 -0
frontend.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import sys
4
+ import tempfile
5
+
6
+ import requests
7
+ from flask import Flask, render_template, request
8
+ from werkzeug.utils import secure_filename
9
+
10
+ sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
11
+
12
+ app = Flask(__name__)
13
+ app.secret_key = "LeafmanZSecretKey"
14
+
15
+ API_HOST = "https://dkdaniz-katara.hf.space:5110/api"
16
+
17
+
18
+ # PAGES #
19
+ @app.route("/", methods=["GET", "POST"])
20
+ def home_page():
21
+ if request.method == "POST":
22
+ if "user_prompt" in request.form:
23
+ user_prompt = request.form["user_prompt"]
24
+ print(f"User Prompt: {user_prompt}")
25
+
26
+ main_prompt_url = f"{API_HOST}/prompt_route"
27
+ response = requests.post(main_prompt_url, data={"user_prompt": user_prompt})
28
+ print(response.status_code) # print HTTP response status code for debugging
29
+ if response.status_code == 200:
30
+ # print(response.json()) # Print the JSON data from the response
31
+ return render_template("home.html", show_response_modal=True, response_dict=response.json())
32
+ elif "documents" in request.files:
33
+ delete_source_url = f"{API_HOST}/delete_source" # URL of the /api/delete_source endpoint
34
+ if request.form.get("action") == "reset":
35
+ response = requests.get(delete_source_url)
36
+
37
+ save_document_url = f"{API_HOST}/save_document"
38
+ run_ingest_url = f"{API_HOST}/run_ingest" # URL of the /api/run_ingest endpoint
39
+ files = request.files.getlist("documents")
40
+ for file in files:
41
+ print(file.filename)
42
+ filename = secure_filename(file.filename)
43
+ with tempfile.SpooledTemporaryFile() as f:
44
+ f.write(file.read())
45
+ f.seek(0)
46
+ response = requests.post(save_document_url, files={"document": (filename, f)})
47
+ print(response.status_code) # print HTTP response status code for debugging
48
+ # Make a GET request to the /api/run_ingest endpoint
49
+ response = requests.get(run_ingest_url)
50
+ print(response.status_code) # print HTTP response status code for debugging
51
+
52
+ # Display the form for GET request
53
+ return render_template(
54
+ "home.html",
55
+ show_response_modal=False,
56
+ response_dict={"Prompt": "None", "Answer": "None", "Sources": [("ewf", "wef")]},
57
+ )
58
+
59
+
60
+ if __name__ == "__main__":
61
+ parser = argparse.ArgumentParser()
62
+ parser.add_argument("--port", type=int, default=5111, help="Port to run the UI on. Defaults to 5111.")
63
+ parser.add_argument(
64
+ "--host",
65
+ type=str,
66
+ default="0.0.0.0",
67
+ help="Host to run the UI on. Defaults to 127.0.0.1. "
68
+ "Set to 0.0.0.0 to make the UI externally "
69
+ "accessible from other devices.",
70
+ )
71
+ args = parser.parse_args()
72
+ app.run(debug=False, host=args.host, port=args.port)