SatyamSinghal commited on
Commit
3ad4486
·
verified ·
1 Parent(s): fc99fb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py CHANGED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Define Hugging Face API endpoint and token
5
+ API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
6
+ API_TOKEN = "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" # Replace with your actual token
7
+
8
+ # Function to query Hugging Face API for text summarization
9
+ def query_summarization(input_text):
10
+ headers = {
11
+ "Authorization": f"Bearer {API_TOKEN}",
12
+ "Content-Type": "application/json",
13
+ }
14
+ payload = {
15
+ "inputs": input_text
16
+ }
17
+ try:
18
+ # Sending POST request to the Hugging Face API
19
+ response = requests.post(API_URL, headers=headers, json=payload)
20
+ response.raise_for_status() # Raise error for bad responses (4xx, 5xx)
21
+ result = response.json()
22
+
23
+ # Extract and return the summary
24
+ if result and "summary_text" in result[0]:
25
+ return result[0]["summary_text"]
26
+ else:
27
+ return "Sorry, an error occurred. Please try again later."
28
+ except requests.exceptions.RequestException as e:
29
+ return f"Error: {e}"
30
+
31
+ # Create Gradio interface for text summarization
32
+ iface = gr.Interface(
33
+ fn=query_summarization, # Function to call
34
+ inputs=gr.Textbox(lines=10, placeholder="Enter your text here..."), # Input text area
35
+ outputs=gr.Textbox(placeholder="Summary will appear here..."), # Output summary text
36
+ title="Text Summarization with Hugging Face",
37
+ description="Enter text to get a summarized version using Hugging Face's BART model.",
38
+ live=True # Set to True for live updates (optional)
39
+ )
40
+
41
+ # Launch the interface
42
+ iface.launch()