Arunvarma2565 commited on
Commit
2feea59
Β·
verified Β·
1 Parent(s): 52938c3

Add app.py - Gradio Blocks UI

Browse files
Files changed (1) hide show
  1. app.py +241 -0
app.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app.py β€” Gradio Blocks UI for Research Draft.
3
+
4
+ Provides a clean, academic interface with two roles:
5
+ β€’ Student β€” upload PDF, generate abstract, view result.
6
+ β€’ Researcher β€” same as Student + view history, export results.
7
+
8
+ Launch:
9
+ python app.py
10
+ """
11
+
12
+ import gradio as gr
13
+
14
+ from abstract_service import generate_abstract_from_pdf
15
+ from history_manager import (
16
+ get_history_as_table,
17
+ clear_history,
18
+ export_latest_to_txt,
19
+ export_full_history_to_txt,
20
+ )
21
+
22
+ # ---------------------------------------------------------------------------
23
+ # CSS β€” minimal overrides for a clean academic look
24
+ # ---------------------------------------------------------------------------
25
+
26
+ CUSTOM_CSS = """
27
+ .main-title {
28
+ text-align: center;
29
+ margin-bottom: 0.2em;
30
+ }
31
+ .sub-title {
32
+ text-align: center;
33
+ color: #555;
34
+ font-size: 0.95em;
35
+ margin-top: 0;
36
+ }
37
+ """
38
+
39
+ # ---------------------------------------------------------------------------
40
+ # Event handlers
41
+ # ---------------------------------------------------------------------------
42
+
43
+ def on_role_change(role: str):
44
+ """Toggle visibility of the Researcher-only section."""
45
+ return gr.Column(visible=(role == "Researcher"))
46
+
47
+
48
+ def on_generate(pdf_file, role: str):
49
+ """
50
+ Handle the Generate Abstract button click.
51
+
52
+ Returns: (abstract_text, status_message)
53
+ """
54
+ if pdf_file is None:
55
+ raise gr.Error("Please upload a PDF file first.")
56
+
57
+ # Gradio file path β€” handle both v5 (.name) and v6 (.path)
58
+ file_path = getattr(pdf_file, "path", None) or getattr(pdf_file, "name", None)
59
+ if not file_path:
60
+ raise gr.Error("Could not read the uploaded file path.")
61
+
62
+ try:
63
+ result = generate_abstract_from_pdf(file_path=file_path, role=role)
64
+ except ValueError as exc:
65
+ raise gr.Error(str(exc))
66
+ except RuntimeError as exc:
67
+ raise gr.Error(str(exc))
68
+ except FileNotFoundError as exc:
69
+ raise gr.Error(str(exc))
70
+ except Exception as exc:
71
+ raise gr.Error(f"Unexpected error: {exc}")
72
+
73
+ return result["abstract"], f"βœ… {result['status']}"
74
+
75
+
76
+ def on_view_history():
77
+ """Load history rows for the Dataframe."""
78
+ rows = get_history_as_table()
79
+ if not rows:
80
+ gr.Info("No history entries yet.")
81
+ return rows
82
+
83
+
84
+ def on_clear_history():
85
+ """Wipe the persistent history file and return an empty table."""
86
+ clear_history()
87
+ gr.Info("History cleared.")
88
+ return []
89
+
90
+
91
+ def on_export_latest():
92
+ """Export the latest abstract to a .txt download."""
93
+ path = export_latest_to_txt()
94
+ if path is None:
95
+ raise gr.Error("Nothing to export β€” history is empty.")
96
+ return path
97
+
98
+
99
+ def on_export_full():
100
+ """Export the entire history to a .txt download."""
101
+ path = export_full_history_to_txt()
102
+ if path is None:
103
+ raise gr.Error("Nothing to export β€” history is empty.")
104
+ return path
105
+
106
+
107
+ # ---------------------------------------------------------------------------
108
+ # UI layout (Gradio Blocks)
109
+ # ---------------------------------------------------------------------------
110
+
111
+ with gr.Blocks(title="Research Draft") as demo:
112
+
113
+ # ── Header ────────────────────────────────────────────────────────────
114
+ gr.Markdown(
115
+ "<h1 class='main-title'>πŸ“„ Research Draft</h1>",
116
+ )
117
+ gr.Markdown(
118
+ "<p class='sub-title'>"
119
+ "AI-powered academic abstract generation Β· Local &amp; Private"
120
+ "</p>",
121
+ )
122
+
123
+ # ── Role selector ─────────────────────────────────────────────────────
124
+ with gr.Row():
125
+ role_dropdown = gr.Dropdown(
126
+ choices=["Student", "Researcher"],
127
+ value="Student",
128
+ label="Select your role",
129
+ scale=1,
130
+ interactive=True,
131
+ )
132
+
133
+ gr.Markdown("---")
134
+
135
+ # ── Main generation section (visible to all roles) ────────────────────
136
+ gr.Markdown("### Upload & Generate")
137
+ with gr.Row():
138
+ with gr.Column(scale=2):
139
+ pdf_input = gr.File(
140
+ label="Upload Research Paper (PDF)",
141
+ file_types=[".pdf"],
142
+ file_count="single",
143
+ )
144
+ generate_btn = gr.Button(
145
+ "πŸ” Generate Abstract",
146
+ variant="primary",
147
+ size="lg",
148
+ )
149
+ status_box = gr.Textbox(
150
+ label="Status",
151
+ interactive=False,
152
+ lines=1,
153
+ placeholder="Upload a PDF and click Generate…",
154
+ )
155
+
156
+ with gr.Column(scale=3):
157
+ abstract_output = gr.Textbox(
158
+ label="Generated Abstract",
159
+ interactive=False,
160
+ lines=12,
161
+ buttons=["copy"],
162
+ placeholder="Your generated abstract will appear here…",
163
+ )
164
+
165
+ # ── Researcher-only section ───────────────────────────────────────────
166
+ with gr.Column(visible=False) as researcher_section:
167
+ gr.Markdown("---")
168
+ gr.Markdown("### πŸ”¬ Researcher Tools")
169
+
170
+ with gr.Row():
171
+ view_history_btn = gr.Button("πŸ“‹ View History")
172
+ clear_history_btn = gr.Button("πŸ—‘οΈ Clear History")
173
+ export_latest_btn = gr.Button("πŸ“₯ Export Latest")
174
+ export_full_btn = gr.Button("πŸ“₯ Export Full History")
175
+
176
+ history_table = gr.Dataframe(
177
+ headers=["Timestamp", "Role", "Filename", "Abstract (preview)"],
178
+ datatype=["str", "str", "str", "str"],
179
+ interactive=False,
180
+ wrap=True,
181
+ label="Generation History",
182
+ )
183
+
184
+ export_file = gr.File(
185
+ label="Download Export",
186
+ interactive=False,
187
+ )
188
+
189
+ # ── Footer ────────────────────────────────────────────────────────────
190
+ gr.Markdown(
191
+ "<br><center style='color:#999; font-size:0.8em;'>"
192
+ "Research Draft Β· B.Tech Final Year Project Β· Runs 100 % locally"
193
+ "</center>"
194
+ )
195
+
196
+ # ── Event wiring ──────────────────────────────────────────────────────
197
+
198
+ # Role switch β†’ show/hide researcher section
199
+ role_dropdown.change(
200
+ fn=on_role_change,
201
+ inputs=role_dropdown,
202
+ outputs=researcher_section,
203
+ )
204
+
205
+ # Generate abstract
206
+ generate_btn.click(
207
+ fn=on_generate,
208
+ inputs=[pdf_input, role_dropdown],
209
+ outputs=[abstract_output, status_box],
210
+ )
211
+
212
+ # Researcher tools
213
+ view_history_btn.click(
214
+ fn=on_view_history,
215
+ outputs=history_table,
216
+ )
217
+ clear_history_btn.click(
218
+ fn=on_clear_history,
219
+ outputs=history_table,
220
+ )
221
+ export_latest_btn.click(
222
+ fn=on_export_latest,
223
+ outputs=export_file,
224
+ )
225
+ export_full_btn.click(
226
+ fn=on_export_full,
227
+ outputs=export_file,
228
+ )
229
+
230
+
231
+ # ---------------------------------------------------------------------------
232
+ # Entry point
233
+ # ---------------------------------------------------------------------------
234
+
235
+ if __name__ == "__main__":
236
+ demo.launch(
237
+ server_name="0.0.0.0",
238
+ server_port=7860,
239
+ show_error=True,
240
+ css=CUSTOM_CSS,
241
+ )