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 )