File size: 1,403 Bytes
e4052a4
 
d96f2ce
e4052a4
d96f2ce
e4052a4
 
d96f2ce
0313cd7
e4052a4
d96f2ce
e4052a4
 
d96f2ce
e4052a4
 
 
d96f2ce
c4be13a
e4052a4
 
 
 
 
d96f2ce
e4052a4
d96f2ce
e4052a4
 
 
 
d96f2ce
 
e4052a4
d96f2ce
e4052a4
 
d96f2ce
e4052a4
d96f2ce
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
40
import gradio as gr
import requests
import os  # Import the os module to use environment variables

# Load Hugging Face API Key from Hugging Face Secrets
HF_API_KEY = os.getenv("IBMGraniteTextSummary")  

# IBM Granite 3B model
MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"

# Function for text summarization
def summarize_text(text):
    if not HF_API_KEY:
        return "Error: No API key found. Please check your Hugging Face Secrets."
    
    url = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
    headers = {"Authorization": f"Bearer {HF_API_KEY}"}
    prompt = f"Summarize the following text in 5 sentences. Focus on the key points:\n\n{text}"
    payload = {"inputs": prompt}

    response = requests.post(url, headers=headers, json=payload)
    
    if response.status_code == 200:
        result = response.json()
        return result[0]["generated_text"] if result else "No response received."
    else:
        return f"Error: {response.status_code} - {response.text}"

# Gradio UI
iface = gr.Interface(
    fn=summarize_text,
    inputs=gr.Textbox(label="Enter your text", lines=5),
    outputs=gr.Textbox(label="Summary"),
    title="IBM Granite Text Summarizer",
    description="This tool uses IBM Granite-3B to summarize texts. Enter a long text, and Granite will generate a concise summary!"
)

# Launch the app
if __name__ == "__main__":
    iface.launch()