Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os # Import the os module to use environment variables
|
| 4 |
+
|
| 5 |
+
# Load Hugging Face API Key from Hugging Face Secrets
|
| 6 |
+
HF_API_KEY = os.getenv("IBM-Granite_Text-Summary9")
|
| 7 |
+
|
| 8 |
+
# IBM Granite 3B model
|
| 9 |
+
MODEL_NAME = "ibm-granite/granite-3.1-1b-a400m-instruct"
|
| 10 |
+
|
| 11 |
+
# Function for text summarization
|
| 12 |
+
def summarize_text(text):
|
| 13 |
+
if not HF_API_KEY:
|
| 14 |
+
return "Error: No API key found. Please check your Hugging Face Secrets."
|
| 15 |
+
|
| 16 |
+
url = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
|
| 17 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 18 |
+
prompt = f"Summarize the following text in 5 sentences. Focus on the key points:\n\n{text}"
|
| 19 |
+
payload = {"inputs": prompt}
|
| 20 |
+
|
| 21 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 22 |
+
|
| 23 |
+
if response.status_code == 200:
|
| 24 |
+
result = response.json()
|
| 25 |
+
return result[0]["generated_text"] if result else "No response received."
|
| 26 |
+
else:
|
| 27 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 28 |
+
|
| 29 |
+
# Gradio UI
|
| 30 |
+
iface = gr.Interface(
|
| 31 |
+
fn=summarize_text,
|
| 32 |
+
inputs=gr.Textbox(label="Enter your text", lines=5),
|
| 33 |
+
outputs=gr.Textbox(label="Summary"),
|
| 34 |
+
title="IBM Granite Text Summarizer",
|
| 35 |
+
description="This tool uses IBM Granite-3B to summarize texts. Enter a long text, and Granite will generate a concise summary!"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the app
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
iface.launch()
|