|
import gradio as gr
|
|
import requests
|
|
import os
|
|
|
|
|
|
HF_API_KEY = os.getenv("IBMGraniteTextSummary")
|
|
|
|
|
|
MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"
|
|
|
|
|
|
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}"
|
|
|
|
|
|
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!"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
iface.launch() |