Decoo commited on
Commit
572b5e7
Β·
1 Parent(s): 024a7b9

modified app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -68
app.py CHANGED
@@ -2,13 +2,8 @@ import gradio as gr
2
  import json
3
  import os
4
  from pathlib import Path
5
- from threading import Thread
6
- from flask import Flask, send_from_directory, jsonify
7
- import socket
8
-
9
- # Flask app to serve static files
10
- flask_app = Flask(__name__)
11
 
 
12
  DATA_BASE = "Viewer/Data/Our_system"
13
  FOLDER_MAP = {
14
  "qa_subtopics": f"{DATA_BASE}/QA+Topics",
@@ -17,64 +12,6 @@ FOLDER_MAP = {
17
  "summary_sdgs": f"{DATA_BASE}/Summary+SDG"
18
  }
19
 
20
- @flask_app.route('/Data/<path:filepath>')
21
- def serve_data(filepath):
22
- """Serve JSON files from Data folder"""
23
- return send_from_directory('Viewer/Data', filepath)
24
-
25
- @flask_app.route('/api/list-files/<folder_key>')
26
- def list_files(folder_key):
27
- """List files in a specific folder"""
28
- folder = FOLDER_MAP.get(folder_key)
29
- if not folder or not os.path.exists(folder):
30
- return jsonify([])
31
-
32
- files = [f for f in os.listdir(folder) if f.endswith('_combined_data.json')]
33
- return jsonify(files)
34
-
35
- def find_free_port():
36
- """Find a free port for Flask"""
37
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
38
- s.bind(('', 0))
39
- s.listen(1)
40
- port = s.getsockname()[1]
41
- return port
42
-
43
- def run_flask(port):
44
- """Run Flask server"""
45
- flask_app.run(host='0.0.0.0', port=port, debug=False, use_reloader=False)
46
-
47
- # Start Flask server in background
48
- FLASK_PORT = find_free_port()
49
- flask_thread = Thread(target=run_flask, args=(FLASK_PORT,), daemon=True)
50
- flask_thread.start()
51
-
52
- print(f"Flask server started on port {FLASK_PORT}")
53
-
54
- # Read your original HTML and inject Flask port
55
- with open("Viewer/viewer_v2.html", "r", encoding="utf-8") as f:
56
- original_html = f.read()
57
-
58
- # Modify the folderMap to use Flask server
59
- modified_html = original_html.replace(
60
- 'const folderMap = {',
61
- f'''const FLASK_PORT = {FLASK_PORT};
62
- const folderMap = {{'''
63
- ).replace(
64
- '"./Data/Our_system/QA+Topics"',
65
- f'"http://localhost:{FLASK_PORT}/Data/Our_system/QA+Topics"'
66
- ).replace(
67
- '"./Data/Our_system/QA+SDGs"',
68
- f'"http://localhost:{FLASK_PORT}/Data/Our_system/QA+SDGs"'
69
- ).replace(
70
- '"./Data/Our_system/Summary+Topics"',
71
- f'"http://localhost:{FLASK_PORT}/Data/Our_system/Summary+Topics"'
72
- ).replace(
73
- '"./Data/Our_system/Summary+SDG"',
74
- f'"http://localhost:{FLASK_PORT}/Data/Our_system/Summary+SDG"'
75
- )
76
-
77
- # Feedback functions
78
  def get_available_events():
79
  """Get all available events from JSON files"""
80
  events = set()
@@ -87,6 +24,15 @@ def get_available_events():
87
  events.add(event_name)
88
  return sorted(list(events))
89
 
 
 
 
 
 
 
 
 
 
90
  def save_feedback(event, format_type, view_type, rating, comment):
91
  """Save feedback to JSON file"""
92
  if not event:
@@ -121,13 +67,32 @@ def save_feedback(event, format_type, view_type, rating, comment):
121
 
122
  return "βœ… Thank you for your feedback!"
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  # Create Gradio interface
125
  with gr.Blocks(title="Report Viewer", theme=gr.themes.Soft()) as demo:
126
  gr.Markdown("# πŸ“Š Our Report Viewer")
127
  gr.Markdown("Explore events organized by topics or SDGs")
128
 
129
  with gr.Tab("πŸ“– Viewer"):
130
- gr.HTML(modified_html)
131
 
132
  with gr.Tab("πŸ’¬ Feedback"):
133
  gr.Markdown("### Leave your feedback on the results")
@@ -173,6 +138,7 @@ with gr.Blocks(title="Report Viewer", theme=gr.themes.Soft()) as demo:
173
  )
174
 
175
  if __name__ == "__main__":
176
- import time
177
- time.sleep(1) # Wait for Flask to start
178
- demo.launch(server_port=7860)
 
 
2
  import json
3
  import os
4
  from pathlib import Path
 
 
 
 
 
 
5
 
6
+ # Data folder paths
7
  DATA_BASE = "Viewer/Data/Our_system"
8
  FOLDER_MAP = {
9
  "qa_subtopics": f"{DATA_BASE}/QA+Topics",
 
12
  "summary_sdgs": f"{DATA_BASE}/Summary+SDG"
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def get_available_events():
16
  """Get all available events from JSON files"""
17
  events = set()
 
24
  events.add(event_name)
25
  return sorted(list(events))
26
 
27
+ def get_folder_index(folder_key):
28
+ """Create index.json for a folder"""
29
+ folder = FOLDER_MAP.get(folder_key)
30
+ if not folder or not os.path.exists(folder):
31
+ return []
32
+
33
+ files = [f for f in os.listdir(folder) if f.endswith('_combined_data.json')]
34
+ return files
35
+
36
  def save_feedback(event, format_type, view_type, rating, comment):
37
  """Save feedback to JSON file"""
38
  if not event:
 
67
 
68
  return "βœ… Thank you for your feedback!"
69
 
70
+ # Create index.json files for each folder
71
+ print("Creating index files...")
72
+ for key, folder in FOLDER_MAP.items():
73
+ if os.path.exists(folder):
74
+ index_path = os.path.join(folder, "index.json")
75
+ files = get_folder_index(key)
76
+ with open(index_path, 'w') as f:
77
+ json.dump(files, f)
78
+ print(f"Created {index_path} with {len(files)} files")
79
+
80
+ # Read your original HTML
81
+ html_path = "Viewer/viewer_v2.html"
82
+ if os.path.exists(html_path):
83
+ with open(html_path, "r", encoding="utf-8") as f:
84
+ original_html = f.read()
85
+ else:
86
+ print(f"WARNING: {html_path} not found!")
87
+ original_html = "<h1>Error: viewer_v2.html not found</h1>"
88
+
89
  # Create Gradio interface
90
  with gr.Blocks(title="Report Viewer", theme=gr.themes.Soft()) as demo:
91
  gr.Markdown("# πŸ“Š Our Report Viewer")
92
  gr.Markdown("Explore events organized by topics or SDGs")
93
 
94
  with gr.Tab("πŸ“– Viewer"):
95
+ gr.HTML(original_html)
96
 
97
  with gr.Tab("πŸ’¬ Feedback"):
98
  gr.Markdown("### Leave your feedback on the results")
 
138
  )
139
 
140
  if __name__ == "__main__":
141
+ # Launch with file serving enabled
142
+ demo.launch(
143
+ allowed_paths=["Viewer"]
144
+ )