Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
# Define Hugging Face API endpoint and token | |
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn" | |
API_TOKEN = "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" # Replace with your actual token | |
# Function to query Hugging Face API for text summarization | |
def query_summarization(input_text): | |
headers = { | |
"Authorization": f"Bearer {API_TOKEN}", | |
"Content-Type": "application/json", | |
} | |
payload = { | |
"inputs": input_text | |
} | |
try: | |
# Sending POST request to the Hugging Face API | |
response = requests.post(API_URL, headers=headers, json=payload) | |
response.raise_for_status() # Raise error for bad responses (4xx, 5xx) | |
result = response.json() | |
# Extract and return the summary | |
if result and "summary_text" in result[0]: | |
return result[0]["summary_text"] | |
else: | |
return "Sorry, an error occurred. Please try again later." | |
except requests.exceptions.RequestException as e: | |
return f"Error: {e}" | |
# Custom CSS styling | |
custom_css = """ | |
#input-text { | |
font-size: 18px; | |
border: 2px solid #8a2be2; | |
border-radius: 10px; | |
padding: 15px; | |
width: 100%; | |
box-sizing: border-box; | |
} | |
#summary-output { | |
font-size: 18px; | |
border: 2px solid #8a2be2; | |
border-radius: 10px; | |
padding: 15px; | |
width: 100%; | |
box-sizing: border-box; | |
background-color: #f4f4f9; | |
color: #333; | |
} | |
h1 { | |
text-align: center; | |
font-size: 3em; | |
color: #8a2be2; | |
margin-bottom: 30px; | |
} | |
.gradio-container { | |
background-color: #f5f5f5; | |
padding: 30px; | |
border-radius: 15px; | |
box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.1); | |
} | |
.gradio-container .output-textbox { | |
font-family: "Arial", sans-serif; | |
color: #333; | |
font-size: 16px; | |
} | |
.gradio-container .input-textbox { | |
font-family: "Arial", sans-serif; | |
font-size: 16px; | |
} | |
""" | |
# Create Gradio interface with custom CSS | |
iface = gr.Interface( | |
fn=query_summarization, # Function to call | |
inputs=gr.Textbox( | |
lines=10, | |
placeholder="Enter your text here...", | |
label="Input Text", | |
elem_id="input-text" | |
), # Input text area | |
outputs=gr.Textbox( | |
placeholder="Summary will appear here...", | |
label="Summary", | |
elem_id="summary-output" | |
), # Output summary text | |
title="AI Text Summarization", | |
description="Enter text to get a summarized version using Hugging Face's BART model.", | |
css=custom_css # Apply custom CSS here | |
) | |
# Launch the interface | |
iface.launch(share=True) |