Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 datetime import datetime
|
| 8 |
+
|
| 9 |
+
# Simplified quota tracking (no file I/O on Spaces)
|
| 10 |
+
API_QUOTA = {"remaining": 600}
|
| 11 |
+
|
| 12 |
+
def summarize_text(text, model="facebook/bart-large-cnn"):
|
| 13 |
+
"""Optimized for Hugging Face Spaces"""
|
| 14 |
+
try:
|
| 15 |
+
token = os.getenv("HF_API_TOKEN")
|
| 16 |
+
if not token:
|
| 17 |
+
return "⚠️ Add HF_API_TOKEN to Secrets", ""
|
| 18 |
+
|
| 19 |
+
API_QUOTA["remaining"] = max(0, API_QUOTA["remaining"] - 1)
|
| 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 |
+
# Gradio Interface
|
| 38 |
+
with gr.Blocks(
|
| 39 |
+
theme=gr.themes.Glass(),
|
| 40 |
+
title="✨ Summify",
|
| 41 |
+
css=".gradio-container {max-width: 800px}"
|
| 42 |
+
) as app:
|
| 43 |
+
|
| 44 |
+
gr.Markdown("# ✨ Summify - AI Text Summarizer")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
input_txt = gr.Textbox(label="Your Text", lines=8, placeholder="Paste text here...")
|
| 48 |
+
output_txt = gr.Textbox(label="Summary", lines=8, interactive=False)
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
btn = gr.Button("Summarize", variant="primary")
|
| 52 |
+
quota = gr.Textbox(label="API Status", interactive=False)
|
| 53 |
+
|
| 54 |
+
btn.click(
|
| 55 |
+
fn=summarize_text,
|
| 56 |
+
inputs=input_txt,
|
| 57 |
+
outputs=[output_txt, quota]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
gr.Examples(
|
| 61 |
+
examples=[
|
| 62 |
+
"The quick brown fox jumps over the lazy dog.",
|
| 63 |
+
"He prayed Fajr half-asleep, unaware of the divine protection awaiting him..."
|
| 64 |
+
],
|
| 65 |
+
inputs=input_txt
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
app.launch()
|