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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
|
| 4 |
+
# Target Space API
|
| 5 |
+
API_URL = "ellamind/sui-demo"
|
| 6 |
+
|
| 7 |
+
# ============================================================================
|
| 8 |
+
# Theme & Styling (Fixed for Dark Mode)
|
| 9 |
+
# ============================================================================
|
| 10 |
+
|
| 11 |
+
THEME = gr.themes.Soft(
|
| 12 |
+
primary_hue="slate",
|
| 13 |
+
secondary_hue="slate",
|
| 14 |
+
neutral_hue="slate",
|
| 15 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 16 |
+
).set(
|
| 17 |
+
block_radius="0.75rem",
|
| 18 |
+
block_shadow="0 1px 3px 0 rgb(0 0 0 / 0.1)",
|
| 19 |
+
button_primary_background_fill="*primary_800",
|
| 20 |
+
button_primary_background_fill_hover="*primary_700",
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
CSS = """
|
| 24 |
+
.gradio-container {
|
| 25 |
+
max-width: 1100px !important;
|
| 26 |
+
margin: 0 auto !important;
|
| 27 |
+
padding-top: 2rem !important;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.main-header {
|
| 31 |
+
text-align: center;
|
| 32 |
+
margin-bottom: 2rem;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.main-header h1 {
|
| 36 |
+
font-size: 2.25rem;
|
| 37 |
+
font-weight: 800;
|
| 38 |
+
margin-bottom: 0.5rem;
|
| 39 |
+
color: var(--body-text-color);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
.section-title {
|
| 43 |
+
margin-bottom: 1rem !important;
|
| 44 |
+
border-bottom: 1px solid var(--border-color-primary);
|
| 45 |
+
padding-bottom: 0.5rem;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/* Thinking/Reasoning block inside the summary markdown */
|
| 49 |
+
.thinking-section {
|
| 50 |
+
background: var(--secondary-50);
|
| 51 |
+
border-left: 4px solid var(--primary-500);
|
| 52 |
+
padding: 1.25rem;
|
| 53 |
+
margin-bottom: 1rem;
|
| 54 |
+
border-radius: 0 0.75rem 0.75rem 0;
|
| 55 |
+
font-style: italic;
|
| 56 |
+
color: var(--body-text-color-subdued);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
/* Specific fix for dark mode reasoning block */
|
| 60 |
+
[data-theme="dark"] .thinking-section {
|
| 61 |
+
background: var(--neutral-800);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
.sources-panel {
|
| 65 |
+
font-size: 0.9rem !important;
|
| 66 |
+
max-height: 400px;
|
| 67 |
+
overflow-y: auto;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.generate-btn {
|
| 71 |
+
margin-top: 1rem;
|
| 72 |
+
font-weight: 600 !important;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.footer {
|
| 76 |
+
text-align: center;
|
| 77 |
+
margin-top: 3rem;
|
| 78 |
+
padding-bottom: 2rem;
|
| 79 |
+
font-size: 0.85rem;
|
| 80 |
+
opacity: 0.6;
|
| 81 |
+
}
|
| 82 |
+
"""
|
| 83 |
+
|
| 84 |
+
# ============================================================================
|
| 85 |
+
# API Proxy Function
|
| 86 |
+
# ============================================================================
|
| 87 |
+
|
| 88 |
+
def summarize_proxy(pdf_file, language, words, custom_instruction):
|
| 89 |
+
"""Calls the backend Space API and yields results."""
|
| 90 |
+
if not pdf_file:
|
| 91 |
+
yield "Please upload a PDF document.", "", gr.update(visible=False)
|
| 92 |
+
return
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
# Initialize the client pointing to the demo space
|
| 96 |
+
client = Client(API_URL)
|
| 97 |
+
|
| 98 |
+
# Use the 'summarize' API endpoint (index 0 in your config)
|
| 99 |
+
# Note: handle_file is necessary for Gradio 5.0+ to process local files correctly
|
| 100 |
+
job = client.submit(
|
| 101 |
+
pdf_file=handle_file(pdf_file),
|
| 102 |
+
language=language,
|
| 103 |
+
words=words,
|
| 104 |
+
custom_instruction=custom_instruction,
|
| 105 |
+
api_name="/summarize"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Iterate through the generator responses for streaming
|
| 109 |
+
for result in job:
|
| 110 |
+
# Result is a tuple: (formatted_output, sources_md, accordion_update)
|
| 111 |
+
yield result
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
yield f"### API Error\n{str(e)}", "Please ensure the backend Space is active.", gr.update(visible=False)
|
| 115 |
+
|
| 116 |
+
# ============================================================================
|
| 117 |
+
# Interface
|
| 118 |
+
# ============================================================================
|
| 119 |
+
|
| 120 |
+
def create_app():
|
| 121 |
+
with gr.Blocks(title="sui-1-24b", theme=THEME, css=CSS) as app:
|
| 122 |
+
|
| 123 |
+
gr.HTML("""
|
| 124 |
+
<div class="main-header">
|
| 125 |
+
<h1>sui-1-24b</h1>
|
| 126 |
+
<p>Grounded summaries with verifiable source citations</p>
|
| 127 |
+
</div>
|
| 128 |
+
""")
|
| 129 |
+
|
| 130 |
+
with gr.Row(equal_height=False):
|
| 131 |
+
# Left: Inputs
|
| 132 |
+
with gr.Column(scale=2):
|
| 133 |
+
gr.Markdown("### Document", elem_classes=["section-title"])
|
| 134 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath")
|
| 135 |
+
|
| 136 |
+
gr.Markdown("### Options", elem_classes=["section-title"])
|
| 137 |
+
language = gr.Dropdown(
|
| 138 |
+
choices=["English", "German", "Spanish", "French", "Italian"],
|
| 139 |
+
value="German",
|
| 140 |
+
label="Language"
|
| 141 |
+
)
|
| 142 |
+
words = gr.Slider(minimum=100, maximum=600, value=150, step=50, label="Summary Length")
|
| 143 |
+
custom_instruction = gr.Textbox(
|
| 144 |
+
label="Instructions (Optional)",
|
| 145 |
+
placeholder="Focus on key findings...",
|
| 146 |
+
lines=2
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
generate_btn = gr.Button("Generate Summary", variant="primary", elem_classes=["generate-btn"])
|
| 150 |
+
|
| 151 |
+
# Right: Outputs
|
| 152 |
+
with gr.Column(scale=3):
|
| 153 |
+
gr.Markdown("### Summary", elem_classes=["section-title"])
|
| 154 |
+
summary_output = gr.Markdown(
|
| 155 |
+
value="*Upload a PDF to begin.*",
|
| 156 |
+
container=True
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Accordion("View Source Citations", open=False, visible=False) as sources_accordion:
|
| 160 |
+
sources_output = gr.Markdown(elem_classes=["sources-panel"])
|
| 161 |
+
|
| 162 |
+
generate_btn.click(
|
| 163 |
+
fn=summarize_proxy,
|
| 164 |
+
inputs=[pdf_input, language, words, custom_instruction],
|
| 165 |
+
outputs=[summary_output, sources_output, sources_accordion],
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
gr.HTML("""
|
| 169 |
+
<div class="footer">
|
| 170 |
+
Powered by <a href="https://huggingface.co/ellamind/sui-1-24b" target="_blank">ellamind/sui-1-24b</a> via API
|
| 171 |
+
</div>
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
+
return app
|
| 175 |
+
|
| 176 |
+
if __name__ == "__main__":
|
| 177 |
+
create_app().launch()
|