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