File size: 5,082 Bytes
d44ebbc d755dc2 d44ebbc d755dc2 d44ebbc d755dc2 d44ebbc d755dc2 d44ebbc d755dc2 d44ebbc |
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import gradio as gr
from transformers import pipeline
# Initialize the summarization pipeline
summarizer = pipeline(
"summarization",
model="plguillou/t5-base-fr-sum-cnndm",
device="cuda" if gr.device=="cuda" else "cpu"
)
def generate_summary(text: str, min_length: int = 100, max_length: int = 256) -> str:
"""
Generate a summary of the input text using the pipeline
Args:
text (str): Input text to summarize
min_length (int): Minimum length of the summary
max_length (int): Maximum length of the summary
Returns:
str: Generated summary
"""
# Generate summary using the pipeline
summary = summarizer(
text,
max_length=max_length,
min_length=min_length,
num_beams=4,
length_penalty=0.2,
no_repeat_ngram_size=3,
early_stopping=True,
do_sample=False,
temperature=1.0,
repetition_penalty=1.2
)
# Return the generated summary text
return summary[0]['summary_text']
# Create the Gradio interface
with gr.Blocks(title="French Text Summarizer") as demo:
gr.Markdown("# 🇫🇷 French Text Summarizer")
gr.Markdown("Enter your French text below to get a concise summary.")
with gr.Tabs():
with gr.TabItem("Summarizer"):
with gr.Row():
with gr.Column():
input_text = gr.Textbox(
label="Input Text",
placeholder="Paste your French text here...",
lines=10
)
with gr.Row():
min_length = gr.Slider(
minimum=50,
maximum=200,
value=100,
step=10,
label="Minimum Summary Length"
)
max_length = gr.Slider(
minimum=150,
maximum=500,
value=256,
step=10,
label="Maximum Summary Length"
)
submit_btn = gr.Button("Generate Summary")
with gr.Column():
output_text = gr.Textbox(
label="Generated Summary",
lines=10
)
with gr.TabItem("API Documentation"):
gr.Markdown("""
# API Documentation
This Gradio app exposes a REST API that you can use to generate summaries programmatically.
## Endpoint
```
POST /api/predict
```
## Request Format
Send a POST request with the following JSON payload:
```json
{
"data": [
"Your text to summarize",
100, // min_length (optional)
256 // max_length (optional)
]
}
```
## Example using cURL
```bash
curl -X POST "http://localhost:7860/api/predict" \\
-H "Content-Type: application/json" \\
-d '{"data": ["Votre texte à résumer ici..."]}'
```
## Example using Python requests
```python
import requests
response = requests.post(
"http://localhost:7860/api/predict",
json={
"data": [
"Votre texte à résumer ici...",
100, # min_length (optional)
256 # max_length (optional)
]
}
)
summary = response.json()
print(summary)
```
## Response Format
```json
{
"data": ["Generated summary text"],
"duration": 0.123 // Time taken in seconds
}
```
## Error Handling
In case of errors, the API will return appropriate HTTP status codes and error messages in the response body.
## Rate Limiting
Please be mindful of rate limiting and API usage. Consider implementing your own rate limiting if making multiple requests.
""")
# Connect the interface
submit_btn.click(
fn=generate_summary,
inputs=[input_text, min_length, max_length],
outputs=output_text,
api_name="predict" # Enable API access for this function
)
if __name__ == "__main__":
# Launch the app with API access enabled
demo.queue().launch(
server_name="0.0.0.0", # Make it accessible from other machines
server_port=7860, # Specify port
share=True, # Generate a public URL (optional)
enable_queue=True, # Enable queuing for API requests
) |