Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -145,7 +145,9 @@ def create_bibtex_entry(data):
|
|
| 145 |
return bibtex
|
| 146 |
|
| 147 |
def save_bibtex(bibtex_content):
|
| 148 |
-
|
|
|
|
|
|
|
| 149 |
|
| 150 |
class CombinedProcessor:
|
| 151 |
def process(self, user_message):
|
|
@@ -199,7 +201,7 @@ class CombinedProcessor:
|
|
| 199 |
# Create the processor instance
|
| 200 |
processor = CombinedProcessor()
|
| 201 |
|
| 202 |
-
#
|
| 203 |
with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
|
| 204 |
gr.HTML("""<h1 style="text-align:center">Reversed Zotero</h1>""")
|
| 205 |
text_input = gr.Textbox(label="Your text", type="text", lines=10)
|
|
@@ -209,24 +211,52 @@ with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
|
|
| 209 |
bibtex_output = gr.Textbox(label="BibTeX Entries", lines=15)
|
| 210 |
export_button = gr.Button("Export", size="sm")
|
| 211 |
|
|
|
|
|
|
|
| 212 |
text_button.click(processor.process, inputs=text_input, outputs=[bibtex_output])
|
| 213 |
export_button.click(
|
| 214 |
save_bibtex,
|
| 215 |
inputs=[bibtex_output],
|
| 216 |
-
outputs=[
|
| 217 |
-
_js="""
|
| 218 |
-
(bibtex) => {
|
| 219 |
-
const blob = new Blob([bibtex], { type: 'text/plain' });
|
| 220 |
-
const url = URL.createObjectURL(blob);
|
| 221 |
-
const a = document.createElement('a');
|
| 222 |
-
a.href = url;
|
| 223 |
-
a.download = 'bibliography.bib';
|
| 224 |
-
a.click();
|
| 225 |
-
URL.revokeObjectURL(url);
|
| 226 |
-
return [null]; // Return null to avoid updating the invisible gr.File
|
| 227 |
-
}
|
| 228 |
-
"""
|
| 229 |
)
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
if __name__ == "__main__":
|
| 232 |
demo.queue().launch()
|
|
|
|
| 145 |
return bibtex
|
| 146 |
|
| 147 |
def save_bibtex(bibtex_content):
|
| 148 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.bib') as temp_file:
|
| 149 |
+
temp_file.write(bibtex_content)
|
| 150 |
+
return temp_file.name
|
| 151 |
|
| 152 |
class CombinedProcessor:
|
| 153 |
def process(self, user_message):
|
|
|
|
| 201 |
# Create the processor instance
|
| 202 |
processor = CombinedProcessor()
|
| 203 |
|
| 204 |
+
#Gradio interface
|
| 205 |
with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
|
| 206 |
gr.HTML("""<h1 style="text-align:center">Reversed Zotero</h1>""")
|
| 207 |
text_input = gr.Textbox(label="Your text", type="text", lines=10)
|
|
|
|
| 211 |
bibtex_output = gr.Textbox(label="BibTeX Entries", lines=15)
|
| 212 |
export_button = gr.Button("Export", size="sm")
|
| 213 |
|
| 214 |
+
file_output = gr.File(label="BibTeX File", visible=False)
|
| 215 |
+
|
| 216 |
text_button.click(processor.process, inputs=text_input, outputs=[bibtex_output])
|
| 217 |
export_button.click(
|
| 218 |
save_bibtex,
|
| 219 |
inputs=[bibtex_output],
|
| 220 |
+
outputs=[file_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
)
|
| 222 |
|
| 223 |
+
# Add this JavaScript to enable automatic download
|
| 224 |
+
demo.load(None, None, None, _js="""
|
| 225 |
+
function downloadFile(url, filename) {
|
| 226 |
+
const link = document.createElement('a');
|
| 227 |
+
link.href = url;
|
| 228 |
+
link.download = filename;
|
| 229 |
+
document.body.appendChild(link);
|
| 230 |
+
link.click();
|
| 231 |
+
document.body.removeChild(link);
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
// Observe changes to the File component
|
| 235 |
+
const observer = new MutationObserver((mutations) => {
|
| 236 |
+
mutations.forEach((mutation) => {
|
| 237 |
+
if (mutation.type === 'attributes' && mutation.attributeName === 'href') {
|
| 238 |
+
const fileComponent = mutation.target;
|
| 239 |
+
const downloadUrl = fileComponent.href;
|
| 240 |
+
if (downloadUrl && downloadUrl !== '#') {
|
| 241 |
+
downloadFile(downloadUrl, 'bibliography.bib');
|
| 242 |
+
// Reset the href to prevent repeated downloads
|
| 243 |
+
fileComponent.setAttribute('href', '#');
|
| 244 |
+
}
|
| 245 |
+
}
|
| 246 |
+
});
|
| 247 |
+
});
|
| 248 |
+
|
| 249 |
+
// Start observing the File component once it's available
|
| 250 |
+
function observeFileComponent() {
|
| 251 |
+
const fileComponent = document.querySelector('.file-preview');
|
| 252 |
+
if (fileComponent) {
|
| 253 |
+
observer.observe(fileComponent, { attributes: true });
|
| 254 |
+
} else {
|
| 255 |
+
setTimeout(observeFileComponent, 100);
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
observeFileComponent();
|
| 260 |
+
""")
|
| 261 |
if __name__ == "__main__":
|
| 262 |
demo.queue().launch()
|