Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,92 +5,50 @@ from dotenv import load_dotenv
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
| 8 |
-
# 1. Modern Summarization Function
|
| 9 |
def summarize(text):
|
| 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 |
-
<h1 style="font-weight: 800;">✨ Summify Pro</h1>
|
| 51 |
-
<p style="color: #666;">AI-powered text condensation at your fingertips</p>
|
| 52 |
-
</div>
|
| 53 |
-
""")
|
| 54 |
-
|
| 55 |
-
# 4. Dual-Panel Layout
|
| 56 |
-
with gr.Row(equal_height=True):
|
| 57 |
-
with gr.Column():
|
| 58 |
-
input_txt = gr.Textbox(
|
| 59 |
-
label="Your Content",
|
| 60 |
-
placeholder="Paste articles, essays, or documents here...",
|
| 61 |
-
lines=10,
|
| 62 |
-
elem_classes=["fancy-input"]
|
| 63 |
-
)
|
| 64 |
-
gr.Examples(
|
| 65 |
-
examples=[
|
| 66 |
-
"The quick brown fox jumps over the lazy dog...",
|
| 67 |
-
"He prayed Fajr half-asleep, unaware of the divine protection..."
|
| 68 |
-
],
|
| 69 |
-
inputs=input_txt,
|
| 70 |
-
label="Try These:"
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
with gr.Column():
|
| 74 |
-
output_txt = gr.Textbox(
|
| 75 |
-
label="AI Summary",
|
| 76 |
-
interactive=False,
|
| 77 |
-
lines=10,
|
| 78 |
-
elem_classes=["fancy-output"]
|
| 79 |
-
)
|
| 80 |
-
with gr.Row():
|
| 81 |
-
submit_btn = gr.Button("Generate Summary", variant="primary")
|
| 82 |
-
clear_btn = gr.Button("Clear")
|
| 83 |
-
|
| 84 |
-
# 5. Interactive Functions
|
| 85 |
-
submit_btn.click(
|
| 86 |
fn=summarize,
|
| 87 |
inputs=input_txt,
|
| 88 |
-
outputs=output_txt
|
| 89 |
-
)
|
| 90 |
-
clear_btn.click(
|
| 91 |
-
fn=lambda: ("", ""),
|
| 92 |
-
inputs=None,
|
| 93 |
-
outputs=[input_txt, output_txt]
|
| 94 |
)
|
| 95 |
|
| 96 |
-
app.launch()
|
|
|
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
|
|
|
|
| 8 |
def summarize(text):
|
| 9 |
+
"""Foolproof summarization with full error handling"""
|
| 10 |
+
try:
|
| 11 |
+
# 1. Verify token
|
| 12 |
+
token = os.getenv("HF_API_TOKEN")
|
| 13 |
+
if not token:
|
| 14 |
+
return "⚠️ Token missing: Set HF_API_TOKEN in Secrets", ""
|
| 15 |
+
|
| 16 |
+
# 2. Call API with timeout
|
| 17 |
+
response = requests.post(
|
| 18 |
+
"https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
|
| 19 |
+
headers={"Authorization": f"Bearer {token}"},
|
| 20 |
+
json={"inputs": text, "parameters": {"max_length": 50}},
|
| 21 |
+
timeout=30
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# 3. Handle all response cases
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
return response.json()[0].get("summary_text", "⚠️ Empty response"), "✅ Success"
|
| 27 |
+
elif response.status_code == 503:
|
| 28 |
+
return "⚠️ Model is loading... Try again in 30s", "🔄 Loading"
|
| 29 |
+
else:
|
| 30 |
+
return f"⚠️ API Error ({response.status_code})", "❌ Failed"
|
| 31 |
+
|
| 32 |
+
except requests.exceptions.RequestException as e:
|
| 33 |
+
return f"⚠️ Network error: {str(e)}", "❌ Network Issue"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"⚠️ Unexpected error: {str(e)}", "❌ Error"
|
| 36 |
+
|
| 37 |
+
# Gradio UI with error display
|
| 38 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 39 |
+
gr.Markdown("# ✨ Summify (Stable Version)")
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
input_txt = gr.Textbox(label="Input Text", lines=7, placeholder="Paste any text...")
|
| 43 |
+
output_txt = gr.Textbox(label="Summary", lines=7, interactive=False)
|
| 44 |
+
|
| 45 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 46 |
+
btn = gr.Button("Summarize", variant="primary")
|
| 47 |
+
|
| 48 |
+
btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
fn=summarize,
|
| 50 |
inputs=input_txt,
|
| 51 |
+
outputs=[output_txt, status]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
app.launch()
|