Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,9 +33,9 @@ def summarize_text(text, model=DEFAULT_MODEL):
|
|
| 33 |
load_dotenv()
|
| 34 |
token = os.getenv("HF_API_TOKEN")
|
| 35 |
if not token:
|
| 36 |
-
return "⚠️ Add your Hugging Face token to .env file"
|
| 37 |
except Exception as e:
|
| 38 |
-
return f"⚠️ Config error: {str(e)}"
|
| 39 |
|
| 40 |
api_url = f"https://api-inference.huggingface.co/models/{model}"
|
| 41 |
headers = {"Authorization": f"Bearer {token}"}
|
|
@@ -53,16 +53,16 @@ def summarize_text(text, model=DEFAULT_MODEL):
|
|
| 53 |
if response.status_code == 200:
|
| 54 |
result = response.json()
|
| 55 |
if isinstance(result, list):
|
| 56 |
-
return result[0].get("summary_text", "⚠️ No summary generated")
|
| 57 |
if response.status_code == 503:
|
| 58 |
time.sleep(RETRY_DELAY)
|
| 59 |
continue
|
| 60 |
-
return f"⚠️ API Error: {response.text[:200]}"
|
| 61 |
except requests.exceptions.RequestException as e:
|
| 62 |
if attempt == MAX_RETRIES - 1:
|
| 63 |
-
return f"⚠️ Network error: {str(e)}"
|
| 64 |
time.sleep(RETRY_DELAY)
|
| 65 |
-
return "⚠️ Failed after multiple attempts"
|
| 66 |
|
| 67 |
# --- Gradio Interface ---
|
| 68 |
css = """
|
|
@@ -70,6 +70,8 @@ button {
|
|
| 70 |
background: #6A0DAD !important;
|
| 71 |
color: white !important;
|
| 72 |
border: none !important;
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
button:hover {
|
| 75 |
background: #8A2BE2 !important;
|
|
@@ -82,14 +84,14 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, title="My AI Summarizer") as app
|
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
input_text = gr.Textbox(label="Input Text", lines=8, placeholder="Paste your article/story here...")
|
| 85 |
-
output_text = gr.Textbox(label="Summary", lines=8)
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
submit_btn = gr.Button("Summarize", variant="primary")
|
| 89 |
quota_display = gr.Textbox(label="API Status", interactive=False)
|
| 90 |
|
| 91 |
submit_btn.click(
|
| 92 |
-
fn=
|
| 93 |
inputs=input_text,
|
| 94 |
outputs=[output_text, quota_display]
|
| 95 |
)
|
|
|
|
| 33 |
load_dotenv()
|
| 34 |
token = os.getenv("HF_API_TOKEN")
|
| 35 |
if not token:
|
| 36 |
+
return "⚠️ Add your Hugging Face token to .env file", "0/600"
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"⚠️ Config error: {str(e)}", "0/600"
|
| 39 |
|
| 40 |
api_url = f"https://api-inference.huggingface.co/models/{model}"
|
| 41 |
headers = {"Authorization": f"Bearer {token}"}
|
|
|
|
| 53 |
if response.status_code == 200:
|
| 54 |
result = response.json()
|
| 55 |
if isinstance(result, list):
|
| 56 |
+
return result[0].get("summary_text", "⚠️ No summary generated"), f"{quota['remaining']}/600"
|
| 57 |
if response.status_code == 503:
|
| 58 |
time.sleep(RETRY_DELAY)
|
| 59 |
continue
|
| 60 |
+
return f"⚠️ API Error: {response.text[:200]}", f"{quota['remaining']}/600"
|
| 61 |
except requests.exceptions.RequestException as e:
|
| 62 |
if attempt == MAX_RETRIES - 1:
|
| 63 |
+
return f"⚠️ Network error: {str(e)}", "0/600"
|
| 64 |
time.sleep(RETRY_DELAY)
|
| 65 |
+
return "⚠️ Failed after multiple attempts", "0/600"
|
| 66 |
|
| 67 |
# --- Gradio Interface ---
|
| 68 |
css = """
|
|
|
|
| 70 |
background: #6A0DAD !important;
|
| 71 |
color: white !important;
|
| 72 |
border: none !important;
|
| 73 |
+
border-radius: 8px !important;
|
| 74 |
+
padding: 12px 24px !important;
|
| 75 |
}
|
| 76 |
button:hover {
|
| 77 |
background: #8A2BE2 !important;
|
|
|
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
input_text = gr.Textbox(label="Input Text", lines=8, placeholder="Paste your article/story here...")
|
| 87 |
+
output_text = gr.Textbox(label="Summary", lines=8, interactive=False)
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
submit_btn = gr.Button("Summarize", variant="primary")
|
| 91 |
quota_display = gr.Textbox(label="API Status", interactive=False)
|
| 92 |
|
| 93 |
submit_btn.click(
|
| 94 |
+
fn=summarize_text,
|
| 95 |
inputs=input_text,
|
| 96 |
outputs=[output_text, quota_display]
|
| 97 |
)
|