Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,96 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
import requests
|
| 6 |
import gradio as gr
|
| 7 |
-
from
|
| 8 |
|
| 9 |
-
|
| 10 |
-
API_QUOTA = {"remaining": 600}
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
response = requests.post(
|
| 22 |
-
f"https://api-inference.huggingface.co/models/{model}",
|
| 23 |
-
headers={"Authorization": f"Bearer {token}"},
|
| 24 |
-
json={"inputs": text, "parameters": {"max_length": 50}}
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
if response.status_code == 200:
|
| 28 |
-
result = response.json()
|
| 29 |
-
if isinstance(result, list):
|
| 30 |
-
return result[0].get("summary_text", "⚠️ No summary"), f"Remaining: {API_QUOTA['remaining']}/600"
|
| 31 |
-
|
| 32 |
-
return f"⚠️ Error: {response.text[:200]}", f"Remaining: {API_QUOTA['remaining']}/600"
|
| 33 |
-
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"⚠️ Error: {str(e)}", ""
|
| 36 |
|
| 37 |
-
#
|
| 38 |
with gr.Blocks(
|
| 39 |
-
theme=gr.themes.
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
) as app:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
inputs=input_txt,
|
| 57 |
-
outputs=
|
| 58 |
)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"He prayed Fajr half-asleep, unaware of the divine protection awaiting him..."
|
| 64 |
-
],
|
| 65 |
-
inputs=input_txt
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
app.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
load_dotenv()
|
|
|
|
| 7 |
|
| 8 |
+
# 1. Modern Summarization Function
|
| 9 |
+
def summarize(text):
|
| 10 |
+
headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
|
| 11 |
+
response = requests.post(
|
| 12 |
+
"https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
|
| 13 |
+
headers=headers,
|
| 14 |
+
json={"inputs": text, "parameters": {"max_length": 50}}
|
| 15 |
+
)
|
| 16 |
+
return response.json()[0]["summary_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# 2. Sexy UI Configuration
|
| 19 |
with gr.Blocks(
|
| 20 |
+
theme=gr.themes.Soft(
|
| 21 |
+
primary_hue="violet",
|
| 22 |
+
secondary_hue="emerald",
|
| 23 |
+
font=[gr.themes.GoogleFont("Poppins"), "sans-serif"]
|
| 24 |
+
),
|
| 25 |
+
css="""
|
| 26 |
+
.gradio-container {
|
| 27 |
+
max-width: 800px;
|
| 28 |
+
margin: 0 auto;
|
| 29 |
+
border-radius: 20px !important;
|
| 30 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1) !important;
|
| 31 |
+
}
|
| 32 |
+
textarea {
|
| 33 |
+
min-height: 200px !important;
|
| 34 |
+
border-radius: 12px !important;
|
| 35 |
+
}
|
| 36 |
+
button {
|
| 37 |
+
background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%) !important;
|
| 38 |
+
border: none !important;
|
| 39 |
+
}
|
| 40 |
+
.example-image {
|
| 41 |
+
border-radius: 12px;
|
| 42 |
+
overflow: hidden;
|
| 43 |
+
}
|
| 44 |
+
"""
|
| 45 |
) as app:
|
| 46 |
+
|
| 47 |
+
# 3. Premium Header
|
| 48 |
+
gr.Markdown("""
|
| 49 |
+
<div style="text-align: center;">
|
| 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()
|
|
|