Sari95's picture
Change comments to english
d96f2ce verified
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()