Spaces:
Running
Running
Download app.py from Authot/punctuation: direct link, hf CLI and curl.
- Browser
- Download file 4.28 kB
-
https://huggingface.co/spaces/Authot/punctuation/resolve/main/app.py
- Command line
-
hf download hf://spaces/Authot/punctuation/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Authot/punctuation/resolve/main/app.py
4.28 kB
| import gradio as gr | |
| import time | |
| import requests | |
| import tempfile | |
| import os | |
| # Récupérer le token d'API depuis une variable d'environnement | |
| API_TOKEN = os.getenv("API_TOKEN") | |
| API_URL = "https://aura.authot.app/api/v1/punctuates" | |
| def submit_punctuation_task(text, lang="fr"): | |
| print("Création d'un fichier temporaire pour le texte...") | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8') as temp_file: | |
| temp_file.write(text) | |
| temp_file_path = temp_file.name | |
| print(f"Fichier temporaire créé : {temp_file_path}") | |
| url = f"{API_URL}" | |
| headers = { | |
| "Authorization": f"Bearer {API_TOKEN}" | |
| } | |
| print(f"Envoi de la requête POST à {url}...") | |
| with open(temp_file_path, 'rb') as file: | |
| files = { | |
| "punctuate[file]": file | |
| } | |
| data = { | |
| "punctuate[lang]": lang | |
| } | |
| response = requests.post(url, headers=headers, files=files, data=data) | |
| os.remove(temp_file_path) | |
| print("Fichier temporaire supprimé.") | |
| if response.ok: | |
| print("Requête POST réussie.") | |
| else: | |
| print(f"Erreur lors de la requête POST : {response.status_code}") | |
| response.raise_for_status() | |
| return response.json() | |
| def check_task_status(task_id): | |
| url = f"{API_URL}/{task_id}" | |
| headers = { | |
| "Authorization": f"Bearer {API_TOKEN}" | |
| } | |
| print(f"Vérification du statut de la tâche {task_id}...") | |
| response = requests.get(url, headers=headers) | |
| if response.ok: | |
| print(f"Statut de la tâche {task_id} récupéré avec succès.") | |
| else: | |
| print(f"Erreur lors de la vérification du statut : {response.status_code}") | |
| response.raise_for_status() | |
| return response.json() | |
| def get_task_result(task_id): | |
| url = f"{API_URL}/{task_id}/result" | |
| headers = { | |
| "Authorization": f"Bearer {API_TOKEN}" | |
| } | |
| print(f"Téléchargement du résultat de la tâche {task_id}...") | |
| response = requests.get(url, headers=headers, allow_redirects=True) | |
| if response.ok: | |
| print(f"Résultat de la tâche {task_id} téléchargé avec succès.") | |
| else: | |
| print(f"Erreur lors du téléchargement du résultat : {response.status_code}") | |
| response.raise_for_status() | |
| return response.text | |
| def greet(name, progress=gr.Progress()): | |
| progress(0, desc="Démarrage") | |
| print("Début du traitement de la ponctuation...") | |
| time.sleep(1) | |
| progress(0.05) | |
| task_info = submit_punctuation_task(name) | |
| task_id = task_info["data"]["id"] | |
| print(f"Tâche de ponctuation soumise avec l'ID : {task_id}") | |
| while True: | |
| task_status = check_task_status(task_id) | |
| status = task_status["data"]["attributes"]["status"] | |
| progress(0.5, desc=f"Statut de la tâche : {status}") | |
| print(f"Statut actuel de la tâche : {status}") | |
| if status == "finished": | |
| print("Tâche terminée. Récupération du résultat...") | |
| break | |
| elif status in ["failed", "canceled"]: | |
| print("La tâche a échoué ou a été annulée.") | |
| return "La tâche a échoué ou a été annulée." | |
| time.sleep(2) | |
| result_text = get_task_result(task_id) | |
| print("Résultat récupéré avec succès.") | |
| return result_text | |
| # Définir un thème personnalisé | |
| custom_theme = gr.themes.Default( | |
| primary_hue="amber", | |
| secondary_hue="pink", | |
| neutral_hue="gray", | |
| font=["sans-serif", "Arial", "Helvetica"], | |
| ).set( | |
| background_fill_primary="#FEF3E6", | |
| background_fill_secondary="#FEF3E6", | |
| ) | |
| # Créer l'application Gradio avec le thème personnalisé | |
| with gr.Blocks(theme=custom_theme) as demo: | |
| gr.HTML(""" | |
| <style> | |
| footer {display: none !important;} | |
| </style> | |
| """) | |
| gr.Markdown( | |
| """ | |
| # Outil gratuit pour améliorer la ponctuation de votre texte ! | |
| Insérez votre texte. Cliquez sur le bouton Envoyer. Récupérez votre texte avec un niveau de ponctuation supérieur. | |
| """) | |
| name = gr.Textbox(label="Texte à corriger") | |
| output = gr.Textbox(label="Votre texte avec la ponctuation améliorée") | |
| greet_btn = gr.Button("Envoyer") | |
| greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet") | |
| # Lancer l'application | |
| demo.launch() | |