File size: 1,168 Bytes
77b3501
165652d
38b9bca
77b3501
21cdd54
134e421
165652d
67013df
21cdd54
165652d
67013df
77b3501
38b9bca
 
 
 
 
 
 
165652d
 
 
38b9bca
 
 
 
 
 
 
 
 
 
 
165652d
 
77b3501
165652d
77b3501
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
from dotenv import load_dotenv
import requests
import gradio as gr


# Load environment variables
load_dotenv()

# Access the Hugging Face API key
hf_api_key = os.getenv('HF_API_KEY')

# Set up the API endpoint
API_URL = "https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6"
headers = {"Authorization": f"Bearer {hf_api_key}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

def summarize(input_text):
    try:
        output = query({
            "inputs": input_text,
            "parameters": {"max_length": 130, "min_length": 30}
        })
        
        if isinstance(output, list) and len(output) > 0 and 'summary_text' in output[0]:
            return output[0]['summary_text']
        elif isinstance(output, dict) and 'error' in output:
            return f"API Error: {output['error']}"
        else:
            return f"Unexpected response format: {output}"
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Create and launch the Gradio interface
demo = gr.Interface(fn=summarize, inputs="text", outputs="text")
demo.launch()