Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,87 +3,133 @@ import requests
|
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
-
#
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
token = os.getenv("HF_API_TOKEN", "").strip()
|
| 14 |
-
if not token.startswith("hf_"):
|
| 15 |
-
return "🔒 Invalid token format - Check Secrets", "❌ Error"
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
if validation.status_code == 401:
|
| 25 |
-
return "🔑 Token rejected - Generate new READ token", "❌ Auth Failed"
|
| 26 |
-
|
| 27 |
-
# 3. Main summarization
|
| 28 |
-
response = requests.post(
|
| 29 |
-
"https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
|
| 30 |
-
headers={"Authorization": f"Bearer {token}"},
|
| 31 |
-
json={
|
| 32 |
-
"inputs": text[:5000], # Character limit for safety
|
| 33 |
-
"parameters": {"max_length": 150}
|
| 34 |
-
},
|
| 35 |
-
timeout=30
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
# 4. Handle all response cases
|
| 39 |
-
if response.status_code == 200:
|
| 40 |
-
summary = response.json()[0].get("summary_text", "No summary generated")
|
| 41 |
-
return summary, "✅ Success"
|
| 42 |
-
elif response.status_code == 503:
|
| 43 |
-
return "🔄 Model loading... Please wait 30 seconds", "⚠️ Retry"
|
| 44 |
-
else:
|
| 45 |
-
return f"API Error {response.status_code}", "❌ Failed"
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Gradio Interface
|
| 53 |
with gr.Blocks(
|
| 54 |
title="Summify Pro",
|
| 55 |
theme=gr.themes.Soft(
|
| 56 |
-
primary_hue="
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
) as app:
|
| 60 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
with gr.Row():
|
| 62 |
input_box = gr.Textbox(
|
| 63 |
-
label="
|
| 64 |
-
placeholder="Paste
|
| 65 |
-
lines=
|
| 66 |
-
max_lines=20
|
|
|
|
| 67 |
)
|
| 68 |
output_box = gr.Textbox(
|
| 69 |
-
label="
|
| 70 |
interactive=False,
|
| 71 |
-
lines=
|
|
|
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
"Generate Summary",
|
| 76 |
-
variant="primary"
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
inputs=input_box,
|
| 80 |
outputs=[output_box, status]
|
| 81 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
#
|
| 84 |
if __name__ == "__main__":
|
| 85 |
app.launch(
|
| 86 |
server_name="0.0.0.0",
|
| 87 |
server_port=7860,
|
| 88 |
-
|
|
|
|
| 89 |
)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
+
# Initialize environment
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
class Summarizer:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.model_url = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
| 12 |
+
self.token = os.getenv("HF_API_TOKEN", "").strip()
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def validate_token(self):
|
| 15 |
+
"""Secure token verification with debug logging"""
|
| 16 |
+
if not self.token.startswith("hf_"):
|
| 17 |
+
print("❌ Token missing 'hf_' prefix") # Debug to logs
|
| 18 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
test = requests.get(
|
| 22 |
+
"https://api-inference.huggingface.co/status",
|
| 23 |
+
headers={"Authorization": f"Bearer {self.token}"},
|
| 24 |
+
timeout=5
|
| 25 |
+
)
|
| 26 |
+
if test.status_code == 401:
|
| 27 |
+
print("🔑 Token rejected by API") # Debug to logs
|
| 28 |
+
return test.ok
|
| 29 |
+
except:
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
def summarize(self, text):
|
| 33 |
+
"""Core summarization with robust error handling"""
|
| 34 |
+
try:
|
| 35 |
+
# Input validation
|
| 36 |
+
if not text.strip():
|
| 37 |
+
return "📝 Please enter text", "⚠️ Warning"
|
| 38 |
+
|
| 39 |
+
# Token check
|
| 40 |
+
if not self.validate_token():
|
| 41 |
+
return "🔒 Invalid token - Check Secrets", "❌ Error"
|
| 42 |
+
|
| 43 |
+
# API Call
|
| 44 |
+
response = requests.post(
|
| 45 |
+
self.model_url,
|
| 46 |
+
headers={"Authorization": f"Bearer {self.token}"},
|
| 47 |
+
json={
|
| 48 |
+
"inputs": text[:5000],
|
| 49 |
+
"parameters": {
|
| 50 |
+
"max_length": 150,
|
| 51 |
+
"min_length": 30,
|
| 52 |
+
"do_sample": False
|
| 53 |
+
}
|
| 54 |
+
},
|
| 55 |
+
timeout=25
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Response handling
|
| 59 |
+
if response.status_code == 200:
|
| 60 |
+
result = response.json()[0].get("summary_text", "No summary generated")
|
| 61 |
+
return result, "✅ Success"
|
| 62 |
+
elif response.status_code == 503:
|
| 63 |
+
return "🔄 Model loading... Try again soon", "⚠️ Retry"
|
| 64 |
+
else:
|
| 65 |
+
return f"API Error {response.status_code}", "❌ Failed"
|
| 66 |
+
|
| 67 |
+
except requests.exceptions.RequestException as e:
|
| 68 |
+
return f"🌐 Network issue: {str(e)}", "⚠️ Connection"
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"Unexpected error: {str(e)}", "❌ Critical"
|
| 71 |
+
|
| 72 |
+
# Initialize summarizer
|
| 73 |
+
summarizer = Summarizer()
|
| 74 |
|
| 75 |
# Gradio Interface
|
| 76 |
with gr.Blocks(
|
| 77 |
title="Summify Pro",
|
| 78 |
theme=gr.themes.Soft(
|
| 79 |
+
primary_hue="indigo",
|
| 80 |
+
secondary_hue="blue",
|
| 81 |
+
font=[gr.themes.GoogleFont("Inter")]
|
| 82 |
+
),
|
| 83 |
+
css=".gradio-container {max-width: 800px !important}"
|
| 84 |
) as app:
|
| 85 |
+
gr.Markdown("""
|
| 86 |
+
# 📝 Summify Pro
|
| 87 |
+
*Secure text summarization powered by Hugging Face*
|
| 88 |
+
""")
|
| 89 |
+
|
| 90 |
with gr.Row():
|
| 91 |
input_box = gr.Textbox(
|
| 92 |
+
label="Your Text",
|
| 93 |
+
placeholder="Paste article, essay, or document...",
|
| 94 |
+
lines=8,
|
| 95 |
+
max_lines=20,
|
| 96 |
+
elem_id="input-box"
|
| 97 |
)
|
| 98 |
output_box = gr.Textbox(
|
| 99 |
+
label="Summary",
|
| 100 |
interactive=False,
|
| 101 |
+
lines=8,
|
| 102 |
+
elem_id="output-box"
|
| 103 |
)
|
| 104 |
+
|
| 105 |
+
status = gr.Textbox(
|
| 106 |
+
label="System Status",
|
| 107 |
+
interactive=False,
|
| 108 |
+
elem_id="status-box"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
submit_btn = gr.Button(
|
| 112 |
"Generate Summary",
|
| 113 |
+
variant="primary",
|
| 114 |
+
elem_id="submit-btn"
|
| 115 |
+
)
|
| 116 |
+
clear_btn = gr.Button("Clear")
|
| 117 |
+
|
| 118 |
+
submit_btn.click(
|
| 119 |
+
fn=summarizer.summarize,
|
| 120 |
inputs=input_box,
|
| 121 |
outputs=[output_box, status]
|
| 122 |
)
|
| 123 |
+
clear_btn.click(
|
| 124 |
+
lambda: ["", "", ""],
|
| 125 |
+
outputs=[input_box, output_box, status]
|
| 126 |
+
)
|
| 127 |
|
| 128 |
+
# Launch configuration
|
| 129 |
if __name__ == "__main__":
|
| 130 |
app.launch(
|
| 131 |
server_name="0.0.0.0",
|
| 132 |
server_port=7860,
|
| 133 |
+
show_error=True,
|
| 134 |
+
debug=True
|
| 135 |
)
|