Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +116 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from gradio_client import Client
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import ast
|
| 7 |
+
|
| 8 |
+
def upload_pdf(pdf_path):
|
| 9 |
+
api_url = "https://tmpfiles.org/api/v1/upload"
|
| 10 |
+
with open(pdf_path, 'rb') as file:
|
| 11 |
+
files = {'file': file}
|
| 12 |
+
response = requests.post(api_url, files=files)
|
| 13 |
+
url = response.json()['data']['url']
|
| 14 |
+
download_url = f"https://tmpfiles.org/dl/{url.split('.org/')[-1]}"
|
| 15 |
+
return download_url
|
| 16 |
+
|
| 17 |
+
def fetch_doi_data(pdf_path):
|
| 18 |
+
pdf_url = upload_pdf(pdf_path)
|
| 19 |
+
client = Client("raannakasturi/ScientryPDFDataAPI")
|
| 20 |
+
result = client.predict(pdf_url=pdf_url, api_name="/getDOIData")
|
| 21 |
+
result = json.loads(result)
|
| 22 |
+
return pdf_url, result
|
| 23 |
+
|
| 24 |
+
def generate_summary_mindmap(pdf_url, doi):
|
| 25 |
+
client = Client("raannakasturi/ScientryAPI")
|
| 26 |
+
result = client.predict(
|
| 27 |
+
url=pdf_url,
|
| 28 |
+
id=doi,
|
| 29 |
+
access_key="scientrypass",
|
| 30 |
+
api_name="/rexplore_summarizer"
|
| 31 |
+
)
|
| 32 |
+
return result
|
| 33 |
+
|
| 34 |
+
def generate_mindmap(markdown_mindmap):
|
| 35 |
+
client = Client("raannakasturi/MindMap")
|
| 36 |
+
result = client.predict(
|
| 37 |
+
mindmap_markdown=markdown_mindmap,
|
| 38 |
+
api_name="/generate"
|
| 39 |
+
)
|
| 40 |
+
return result
|
| 41 |
+
|
| 42 |
+
def create_files(title, content, file_type):
|
| 43 |
+
file_name = f"{title}.{file_type}"
|
| 44 |
+
if isinstance(content, str) and content.startswith("b'"):
|
| 45 |
+
content = ast.literal_eval(content)
|
| 46 |
+
with open(file_name, "wb") as file:
|
| 47 |
+
file.write(content)
|
| 48 |
+
return file_name
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def main(pdf_path):
|
| 52 |
+
pdf_url, doi_data = fetch_doi_data(pdf_path)
|
| 53 |
+
summary_mindmap = generate_summary_mindmap(pdf_url, doi_data["doi"])
|
| 54 |
+
markdown_summary = summary_mindmap[1]
|
| 55 |
+
markdown_mindmap = summary_mindmap[2]
|
| 56 |
+
result = generate_mindmap(markdown_mindmap)
|
| 57 |
+
if not result:
|
| 58 |
+
mindmap_svg_file = ""
|
| 59 |
+
mindmap_pdf_file = ""
|
| 60 |
+
else:
|
| 61 |
+
mindmap_svg_file = create_files(doi_data["title"], result[0], "svg")
|
| 62 |
+
mindmap_pdf_file = create_files(doi_data["title"], result[1], "pdf")
|
| 63 |
+
return doi_data["citation_text"], doi_data["title"], markdown_summary, mindmap_svg_file, mindmap_pdf_file
|
| 64 |
+
|
| 65 |
+
def generate_pdf_summary_mindmap(pdf):
|
| 66 |
+
pdf_path = pdf.name
|
| 67 |
+
citation, title, markdown_summary, mindmap_svg_file, mindmap_pdf_file = main(pdf_path)
|
| 68 |
+
return citation, title, markdown_summary, mindmap_svg_file, mindmap_pdf_file
|
| 69 |
+
|
| 70 |
+
def download_file(download_as_pdf):
|
| 71 |
+
return download_as_pdf
|
| 72 |
+
|
| 73 |
+
with gr.Blocks(title="Scientry Local App") as app:
|
| 74 |
+
gr.HTML("""
|
| 75 |
+
<div
|
| 76 |
+
style="display: flex; padding-top: 2.5rem; flex-direction: column; justify-content: center; align-items: center; width: 100%;">
|
| 77 |
+
<div
|
| 78 |
+
style="display: flex; margin-left: 0.5rem; flex-direction: row; justify-content: center; align-items: center;">
|
| 79 |
+
<img src="https://raw.githubusercontent.com/RaannaKasturi/ScientryLocal/refs/heads/main/icon.png"
|
| 80 |
+
style="margin-right: 0.5rem" alt="ScientryIcon" width="32" height="32">
|
| 81 |
+
<h1 style="font-size: 1.875rem; line-height: 2.25rem; font-weight: 700;">Scientry (Local)</h1>
|
| 82 |
+
</div>
|
| 83 |
+
<p style="font-style: italic;">Science Simplifed, Knowledge Amplified</p>
|
| 84 |
+
<br>
|
| 85 |
+
<p>
|
| 86 |
+
Designed and Developed by
|
| 87 |
+
<a style="text-decoration: underline; text-underline-offset: 2px;" href="http://nayankasturi.eu.org"
|
| 88 |
+
target="_blank" rel="noopener noreferrer">
|
| 89 |
+
Nayan Kasturi
|
| 90 |
+
</a>
|
| 91 |
+
and
|
| 92 |
+
<a style="text-decoration: underline; text-underline-offset: 2px;" href="http://binarybiology.top"
|
| 93 |
+
target="_blank" rel="noopener noreferrer">
|
| 94 |
+
Binary Biology
|
| 95 |
+
</a>
|
| 96 |
+
</p>
|
| 97 |
+
</div>
|
| 98 |
+
""")
|
| 99 |
+
with gr.Column():
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column():
|
| 102 |
+
pdf = gr.File(file_count='single', file_types=[".pdf"], label="Upload PDF")
|
| 103 |
+
generate = gr.Button(value="Generate")
|
| 104 |
+
with gr.Column():
|
| 105 |
+
title = gr.Textbox(label="Title", placeholder="Title will be displayed here", show_copy_button=True)
|
| 106 |
+
citation = gr.Textbox(label="Citation", placeholder="Citation will be displayed here", show_copy_button=True)
|
| 107 |
+
with gr.Row():
|
| 108 |
+
summary = gr.Textbox(label="Summary", placeholder="Summary will be displayed here", interactive=True, show_copy_button=True)
|
| 109 |
+
with gr.Column():
|
| 110 |
+
download_as_svg = gr.DownloadButton(label="Download as SVG")
|
| 111 |
+
download_as_pdf = gr.DownloadButton(label="Download as PDF")
|
| 112 |
+
generate.click(generate_pdf_summary_mindmap, inputs=[pdf], outputs=[citation, title, summary, download_as_svg, download_as_pdf])
|
| 113 |
+
download_as_svg.click(download_file, inputs=[download_as_svg], outputs=[download_as_svg])
|
| 114 |
+
download_as_pdf.click(download_file, inputs=[download_as_pdf], outputs=[download_as_pdf])
|
| 115 |
+
|
| 116 |
+
app.launch(inbrowser=True, show_api=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
gradio-client
|
| 3 |
+
requests
|