File size: 4,990 Bytes
871cfb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

from gradio_client import Client
import json
import requests
import gradio as gr
import ast

def upload_pdf(pdf_path):
    api_url = "https://tmpfiles.org/api/v1/upload"
    with open(pdf_path, 'rb') as file:
        files = {'file': file}
        response = requests.post(api_url, files=files)
    url = response.json()['data']['url']
    download_url = f"https://tmpfiles.org/dl/{url.split('.org/')[-1]}"
    return download_url

def fetch_doi_data(pdf_path):
    pdf_url = upload_pdf(pdf_path)
    client = Client("raannakasturi/ScientryPDFDataAPI")
    result = client.predict(pdf_url=pdf_url, api_name="/getDOIData")
    result = json.loads(result)
    return pdf_url, result

def generate_summary_mindmap(pdf_url, doi):
    client = Client("raannakasturi/ScientryAPI")
    result = client.predict(
        url=pdf_url,
        id=doi,
        access_key="scientrypass",
        api_name="/rexplore_summarizer"
    )
    return result

def generate_mindmap(markdown_mindmap):
    client = Client("raannakasturi/MindMap")
    result = client.predict(
            mindmap_markdown=markdown_mindmap,
            api_name="/generate"
    )
    return result

def create_files(title, content, file_type):
    file_name = f"{title}.{file_type}"
    if isinstance(content, str) and content.startswith("b'"):
        content = ast.literal_eval(content)
    with open(file_name, "wb") as file:
        file.write(content)
    return file_name


def main(pdf_path):
    pdf_url, doi_data = fetch_doi_data(pdf_path)
    summary_mindmap = generate_summary_mindmap(pdf_url, doi_data["doi"])
    markdown_summary = summary_mindmap[1]
    markdown_mindmap = summary_mindmap[2]
    result = generate_mindmap(markdown_mindmap)
    if not result:
        mindmap_svg_file = ""
        mindmap_pdf_file = ""
    else:
        mindmap_svg_file = create_files(doi_data["title"], result[0], "svg")
        mindmap_pdf_file = create_files(doi_data["title"], result[1], "pdf")
    return doi_data["citation_text"], doi_data["title"], markdown_summary, mindmap_svg_file, mindmap_pdf_file

def generate_pdf_summary_mindmap(pdf):
    pdf_path = pdf.name
    citation, title, markdown_summary, mindmap_svg_file, mindmap_pdf_file = main(pdf_path)
    return citation, title, markdown_summary, mindmap_svg_file, mindmap_pdf_file

def download_file(download_as_pdf):
    return download_as_pdf

with gr.Blocks(title="Scientry Local App") as app:
    gr.HTML("""

    <div

        style="display: flex; padding-top: 2.5rem; flex-direction: column; justify-content: center; align-items: center; width: 100%;">

        <div

            style="display: flex; margin-left: 0.5rem; flex-direction: row; justify-content: center; align-items: center;">

            <img src="https://raw.githubusercontent.com/RaannaKasturi/ScientryLocal/refs/heads/main/icon.png"

                style="margin-right: 0.5rem" alt="ScientryIcon" width="32" height="32">

            <h1 style="font-size: 1.875rem; line-height: 2.25rem; font-weight: 700;">Scientry (Local)</h1>

        </div>

        <p style="font-style: italic;">Science Simplifed, Knowledge Amplified</p>

        <br>

        <p>

            Designed and Developed by

            <a style="text-decoration: underline; text-underline-offset: 2px;" href="http://nayankasturi.eu.org"

                target="_blank" rel="noopener noreferrer">

                Nayan Kasturi

            </a>

            and

            <a style="text-decoration: underline; text-underline-offset: 2px;" href="http://binarybiology.top"

                target="_blank" rel="noopener noreferrer">

                Binary Biology

            </a>

        </p>

    </div>

    """)
    with gr.Column():
        with gr.Row():
            with gr.Column():
                pdf = gr.File(file_count='single', file_types=[".pdf"], label="Upload PDF")
                generate = gr.Button(value="Generate")
            with gr.Column():
                title = gr.Textbox(label="Title", placeholder="Title will be displayed here", show_copy_button=True)
                citation = gr.Textbox(label="Citation", placeholder="Citation will be displayed here", show_copy_button=True)
        with gr.Row():
            summary = gr.Textbox(label="Summary", placeholder="Summary will be displayed here", interactive=True, show_copy_button=True)
            with gr.Column():
                download_as_svg = gr.DownloadButton(label="Download as SVG")
                download_as_pdf = gr.DownloadButton(label="Download as PDF")
    generate.click(generate_pdf_summary_mindmap, inputs=[pdf], outputs=[citation, title, summary, download_as_svg, download_as_pdf])
    download_as_svg.click(download_file, inputs=[download_as_svg], outputs=[download_as_svg])
    download_as_pdf.click(download_file, inputs=[download_as_pdf], outputs=[download_as_pdf])

app.launch(inbrowser=True, show_api=False)