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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import sys
|
| 3 |
+
|
| 4 |
+
# sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
|
| 5 |
+
|
| 6 |
+
# import tempfile
|
| 7 |
+
# from pathlib import Path
|
| 8 |
+
# import base64
|
| 9 |
+
# import subprocess
|
| 10 |
+
# import json
|
| 11 |
+
# import gradio as gr
|
| 12 |
+
# import requests
|
| 13 |
+
# from src.custom_main import generate_diagram_demo
|
| 14 |
+
|
| 15 |
+
# # Import your project's modules here
|
| 16 |
+
# # from diagram_generator import generate_diagram
|
| 17 |
+
# # from paper_processor import process_paper
|
| 18 |
+
|
| 19 |
+
# def process_pdf(pdf_file) -> str:
|
| 20 |
+
# """Extract text from PDF file using local API service"""
|
| 21 |
+
# print(f"====> Processing PDF: {pdf_file.name}")
|
| 22 |
+
# try:
|
| 23 |
+
# with open(pdf_file.name, "rb") as f:
|
| 24 |
+
# files = {"file": (pdf_file.name, f, "application/pdf")}
|
| 25 |
+
# response = requests.post(
|
| 26 |
+
# "https://amusing-presently-chow.ngrok-free.app/parse-pdf",
|
| 27 |
+
# files=files
|
| 28 |
+
# )
|
| 29 |
+
# # Call local API service to process PDF
|
| 30 |
+
# # response = requests.post(
|
| 31 |
+
# # "http://amusing-presently-chow.ngrok-free.app/parse-pdf",
|
| 32 |
+
# # json={"pdf_path": pdf_file}
|
| 33 |
+
# # )
|
| 34 |
+
|
| 35 |
+
# # Check if the request was successful
|
| 36 |
+
# response.raise_for_status()
|
| 37 |
+
|
| 38 |
+
# # Parse the JSON response
|
| 39 |
+
# output = response.json()
|
| 40 |
+
# return output
|
| 41 |
+
# except requests.RequestException as e:
|
| 42 |
+
# print(f"Error calling PDF processing API: {e}")
|
| 43 |
+
# return f"Error calling PDF processing API: {str(e)}"
|
| 44 |
+
# except json.JSONDecodeError as e:
|
| 45 |
+
# print(f"Error parsing API response: {e}")
|
| 46 |
+
# return f"Error parsing API response: {response.text}"
|
| 47 |
+
# except Exception as e:
|
| 48 |
+
# print(f"Unexpected error: {e}")
|
| 49 |
+
# return f"Unexpected error: {str(e)}"
|
| 50 |
+
|
| 51 |
+
# def encode_image(image_path: str) -> str:
|
| 52 |
+
# """Encode image to base64 string"""
|
| 53 |
+
# with open(image_path, "rb") as image_file:
|
| 54 |
+
# return base64.b64encode(image_file.read()).decode('utf-8')
|
| 55 |
+
|
| 56 |
+
# def create_demo():
|
| 57 |
+
# """Create and launch the Gradio demo interface"""
|
| 58 |
+
# with gr.Blocks(title="SCISKETCH Demo") as demo:
|
| 59 |
+
# gr.Markdown("SCISKETCH Demo")
|
| 60 |
+
# gr.Markdown("Upload a PDF, provide a caption, and optionally upload images to generate a diagram.")
|
| 61 |
+
|
| 62 |
+
# # State variable to store PDF processing result
|
| 63 |
+
# pdf_text_state = gr.State("")
|
| 64 |
+
|
| 65 |
+
# with gr.Row():
|
| 66 |
+
# with gr.Column(scale=1):
|
| 67 |
+
# pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 68 |
+
# process_pdf_btn = gr.Button("Process PDF", variant="secondary")
|
| 69 |
+
# caption_input = gr.Textbox(label="Caption", placeholder="Enter a caption for the diagram...")
|
| 70 |
+
# image_input = gr.File(label="Upload Images (Optional)", file_types=[".png", ".jpg", ".jpeg"], file_count="multiple")
|
| 71 |
+
# submit_btn = gr.Button("Generate Diagram", variant="primary")
|
| 72 |
+
|
| 73 |
+
# with gr.Column(scale=1):
|
| 74 |
+
# pdf_text_display = gr.Textbox(label="PDF Processing Result", lines=10)
|
| 75 |
+
# output_display = gr.Textbox(label="Final Result", lines=20)
|
| 76 |
+
|
| 77 |
+
# # Process PDF button handler
|
| 78 |
+
# def process_pdf_handler(pdf_file):
|
| 79 |
+
# if pdf_file is None:
|
| 80 |
+
# return "No PDF file uploaded", ""
|
| 81 |
+
|
| 82 |
+
# pdf_text = process_pdf(pdf_file)
|
| 83 |
+
|
| 84 |
+
# # Format the dictionary output for better display
|
| 85 |
+
# if isinstance(pdf_text, dict):
|
| 86 |
+
# formatted_output = "## Paper Information\n\n"
|
| 87 |
+
|
| 88 |
+
# # Display title and authors if available
|
| 89 |
+
# if "title" in pdf_text and pdf_text["title"]:
|
| 90 |
+
# formatted_output += f"**Title:** {pdf_text['title']}\n\n"
|
| 91 |
+
|
| 92 |
+
# if "authors" in pdf_text and pdf_text["authors"]:
|
| 93 |
+
# formatted_output += f"**Authors:** {pdf_text['authors']}\n\n"
|
| 94 |
+
|
| 95 |
+
# if "pub_date" in pdf_text and pdf_text["pub_date"]:
|
| 96 |
+
# formatted_output += f"**Publication Date:** {pdf_text['pub_date']}\n\n"
|
| 97 |
+
|
| 98 |
+
# # Display abstract if available
|
| 99 |
+
# if "abstract" in pdf_text and pdf_text["abstract"]:
|
| 100 |
+
# formatted_output += f"**Abstract:**\n{pdf_text['abstract']}\n\n"
|
| 101 |
+
|
| 102 |
+
# # Display section count if available
|
| 103 |
+
# if "sections" in pdf_text and isinstance(pdf_text["sections"], list):
|
| 104 |
+
# formatted_output += f"**Sections:** {len(pdf_text['sections'])}\n\n"
|
| 105 |
+
|
| 106 |
+
# # Display section headings
|
| 107 |
+
# formatted_output += "**Section Headings:**\n"
|
| 108 |
+
# for i, section in enumerate(pdf_text["sections"]):
|
| 109 |
+
# if "heading" in section and section["heading"]:
|
| 110 |
+
# formatted_output += f"{i+1}. {section['heading']}\n"
|
| 111 |
+
|
| 112 |
+
# # Display reference count if available
|
| 113 |
+
# if "references" in pdf_text and isinstance(pdf_text["references"], list):
|
| 114 |
+
# formatted_output += f"\n**References:** {len(pdf_text['references'])}\n"
|
| 115 |
+
|
| 116 |
+
# # Keep the raw JSON for the state
|
| 117 |
+
# return formatted_output, pdf_text
|
| 118 |
+
# else:
|
| 119 |
+
# # If it's not a dictionary, return as is
|
| 120 |
+
# return str(pdf_text), pdf_text
|
| 121 |
+
|
| 122 |
+
# process_pdf_btn.click(
|
| 123 |
+
# fn=process_pdf_handler,
|
| 124 |
+
# inputs=[pdf_input],
|
| 125 |
+
# outputs=[pdf_text_display, pdf_text_state]
|
| 126 |
+
# )
|
| 127 |
+
|
| 128 |
+
# # Generate diagram button handler
|
| 129 |
+
# def analyze_with_pdf_text(pdf_file, pdf_text, caption, image_files):
|
| 130 |
+
# print(f"Analyzing with PDF_file {pdf_file}, caption {caption}, image_files {image_files}")
|
| 131 |
+
# result = generate_diagram_demo(pdf_text, caption, image_files)
|
| 132 |
+
|
| 133 |
+
# # # Use the processed PDF text
|
| 134 |
+
# # if pdf_text:
|
| 135 |
+
# # result += f"Using processed PDF text ({len(pdf_text)} characters).\n\n"
|
| 136 |
+
# # elif pdf_file is not None:
|
| 137 |
+
# # # Process PDF if not already processed
|
| 138 |
+
# # pdf_text = process_pdf(pdf_file.name)
|
| 139 |
+
# # result += f"Extracted {len(pdf_text)} characters from PDF.\n\n"
|
| 140 |
+
|
| 141 |
+
# # Process caption
|
| 142 |
+
# # if caption:
|
| 143 |
+
# # result += f"Caption: {caption}\n\n"
|
| 144 |
+
|
| 145 |
+
# # Process images if provided
|
| 146 |
+
# # if image_files:
|
| 147 |
+
# # result += f"Processed {len(image_files)} images:\n"
|
| 148 |
+
# # for i, img in enumerate(image_files):
|
| 149 |
+
# # result += f"- Image {i+1}: {Path(img.name).name}\n"
|
| 150 |
+
|
| 151 |
+
# # Here you would call your actual diagram generation code
|
| 152 |
+
# # result = generate_diagram(pdf_text, caption, image_files)
|
| 153 |
+
|
| 154 |
+
# # For demo purposes, return a placeholder
|
| 155 |
+
# # result += "\n\nDiagram generation would happen here based on the inputs."
|
| 156 |
+
|
| 157 |
+
# return result
|
| 158 |
+
|
| 159 |
+
# submit_btn.click(
|
| 160 |
+
# fn=analyze_with_pdf_text,
|
| 161 |
+
# inputs=[pdf_input, pdf_text_state, caption_input, image_input],
|
| 162 |
+
# outputs=output_display
|
| 163 |
+
# )
|
| 164 |
+
|
| 165 |
+
# return demo
|
| 166 |
+
|
| 167 |
+
# if __name__ == "__main__":
|
| 168 |
+
# demo = create_demo()
|
| 169 |
+
# demo.launch(server_port=7860)
|
| 170 |
+
|
| 171 |
+
import gradio as gr
|
| 172 |
+
|
| 173 |
+
def show_link():
|
| 174 |
+
return """
|
| 175 |
+
<div style="text-align: center; font-family: sans-serif; padding: 40px;">
|
| 176 |
+
<h2>This app has moved.</h2>
|
| 177 |
+
<p>Please click the link below to visit the new version:</p>
|
| 178 |
+
<a href="https://amusing-presently-chow.ngrok-free.app/" target="_blank" style="font-size: 18px; color: #1f6feb;">
|
| 179 |
+
👉 Go to the new app
|
| 180 |
+
</a>
|
| 181 |
+
</div>
|
| 182 |
+
"""
|
| 183 |
+
|
| 184 |
+
demo = gr.Interface(fn=show_link, inputs=[], outputs=gr.HTML())
|
| 185 |
+
|
| 186 |
+
demo.launch()
|