Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_URL = "https://api-inference.huggingface.co/models/DISLab/SummLlama3.2-3B"
|
| 6 |
+
|
| 7 |
+
def summarize(text):
|
| 8 |
+
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
| 9 |
+
payload = {"inputs": text}
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 13 |
+
response.raise_for_status()
|
| 14 |
+
result = response.json()
|
| 15 |
+
|
| 16 |
+
# Handle different response formats
|
| 17 |
+
if isinstance(result, list) and len(result) > 0:
|
| 18 |
+
return result[0].get('summary_text', result[0].get('generated_text', str(result[0])))
|
| 19 |
+
elif isinstance(result, dict):
|
| 20 |
+
return result.get('summary_text', result.get('generated_text', str(result)))
|
| 21 |
+
else:
|
| 22 |
+
return str(result)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Error: {str(e)}"
|
| 25 |
+
|
| 26 |
+
# Create Gradio interface
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=summarize,
|
| 29 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter text to summarize..."),
|
| 30 |
+
outputs=gr.Textbox(label="Summary"),
|
| 31 |
+
title="SummLlama3.2-3B Summarization",
|
| 32 |
+
description="Test the DISLab/SummLlama3.2-3B model using Hugging Face Inference API"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|