Authot commited on
Commit
7196d8e
·
verified ·
1 Parent(s): 014f016

Initial Punctuation interface

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import requests
4
+ import tempfile
5
+ import os
6
+
7
+ # Remplacez par votre token d'API
8
+ API_TOKEN = "h1MQgTCRfuTsPfGov2DpKwSxxJyH9somWPb7yW-zE8iX7zvN28BMZH4s4UpQ"
9
+ API_URL = "https://aura.authot.app/punctuates"
10
+
11
+ def submit_punctuation_task(text, lang="fr"):
12
+ print("Création d'un fichier temporaire pour le texte...")
13
+ # Crée un fichier temporaire pour stocker le texte
14
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8') as temp_file:
15
+ temp_file.write(text)
16
+ temp_file_path = temp_file.name
17
+ print(f"Fichier temporaire créé : {temp_file_path}")
18
+
19
+ url = f"{API_URL}"
20
+ headers = {
21
+ "Authorization": f"Bearer {API_TOKEN}"
22
+ }
23
+ print(f"Envoi de la requête POST à {url}...")
24
+ with open(temp_file_path, 'rb') as file:
25
+ files = {
26
+ "punctuate[file]": file
27
+ }
28
+ data = {
29
+ "punctuate[lang]": lang
30
+ }
31
+ response = requests.post(url, headers=headers, files=files, data=data)
32
+
33
+ # Supprime le fichier temporaire après l'envoi
34
+ os.remove(temp_file_path)
35
+ print("Fichier temporaire supprimé.")
36
+
37
+ if response.ok:
38
+ print("Requête POST réussie.")
39
+ else:
40
+ print(f"Erreur lors de la requête POST : {response.status_code}")
41
+
42
+ response.raise_for_status()
43
+ return response.json()
44
+
45
+ def check_task_status(task_id):
46
+ url = f"{API_URL}/{task_id}"
47
+ headers = {
48
+ "Authorization": f"Bearer {API_TOKEN}"
49
+ }
50
+ print(f"Vérification du statut de la tâche {task_id}...")
51
+ response = requests.get(url, headers=headers)
52
+
53
+ if response.ok:
54
+ print(f"Statut de la tâche {task_id} récupéré avec succès.")
55
+ else:
56
+ print(f"Erreur lors de la vérification du statut : {response.status_code}")
57
+
58
+ response.raise_for_status()
59
+ return response.json()
60
+
61
+ def get_task_result(task_id):
62
+ url = f"{API_URL}/{task_id}/result"
63
+ headers = {
64
+ "Authorization": f"Bearer {API_TOKEN}"
65
+ }
66
+ print(f"Téléchargement du résultat de la tâche {task_id}...")
67
+ response = requests.get(url, headers=headers, allow_redirects=True)
68
+
69
+ if response.ok:
70
+ print(f"Résultat de la tâche {task_id} téléchargé avec succès.")
71
+ else:
72
+ print(f"Erreur lors du téléchargement du résultat : {response.status_code}")
73
+
74
+ response.raise_for_status()
75
+ return response.text
76
+
77
+ def greet(name, progress=gr.Progress()):
78
+ progress(0, desc="Démarrage")
79
+ print("Début du traitement de la ponctuation...")
80
+ time.sleep(1)
81
+ progress(0.05)
82
+
83
+ # Submit the punctuation task
84
+ task_info = submit_punctuation_task(name)
85
+ task_id = task_info["data"]["id"]
86
+ print(f"Tâche de ponctuation soumise avec l'ID : {task_id}")
87
+
88
+ # Check the status of the task
89
+ while True:
90
+ task_status = check_task_status(task_id)
91
+ status = task_status["data"]["attributes"]["status"]
92
+ progress(0.5, desc=f"Statut de la tâche : {status}")
93
+ print(f"Statut actuel de la tâche : {status}")
94
+
95
+ if status == "finished":
96
+ print("Tâche terminée. Récupération du résultat...")
97
+ break
98
+ elif status in ["failed", "canceled"]:
99
+ print("La tâche a échoué ou a été annulée.")
100
+ return "La tâche a échoué ou a été annulée."
101
+
102
+ time.sleep(2) # Wait before checking again
103
+
104
+ # Get the result
105
+ result_text = get_task_result(task_id)
106
+ print("Résultat récupéré avec succès.")
107
+ return result_text
108
+
109
+ with gr.Blocks() as demo:
110
+ gr.Markdown(
111
+ """
112
+ # Outil gratuit pour améliorer la ponctuation de votre texte !
113
+ Insérez votre texte. Cliquez sur le bouton Envoyer. Récupérez votre texte avec un niveau de ponctuation supérieur.
114
+ """)
115
+
116
+ name = gr.Textbox(label="Texte à corriger")
117
+ output = gr.Textbox(label="Votre texte avec la ponctuation améliorée")
118
+ greet_btn = gr.Button("Envoyer")
119
+ greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
120
+
121
+ demo.launch()