alvibe75 commited on
Commit
0c59c80
·
verified ·
1 Parent(s): 597f761

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -1,32 +1,40 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import os
4
 
5
- # 🔹 API Key de Hugging Face (debe estar en las Variables del Space)
6
- API_KEY = os.getenv("HF_API_KEY")
7
 
8
- # 🔹 Cliente de Hugging Face Inference
9
- client = InferenceClient(
10
- model="flax-community/spanish-t5-small",
11
- api_key=API_KEY # Usa la API Key de Hugging Face
12
- )
13
-
14
- # 🔒 Clave secreta de acceso
15
  SECRET_KEY = "MI_CLAVE_SUPER_SECRETA"
16
 
17
- # 🔹 Función para reformular el texto
18
  def reformular_texto(frase, clave):
19
  if clave != SECRET_KEY:
20
  return "🔴 Acceso denegado."
21
 
22
- # 🔥 Prompt mejorado
23
- prompt = f"Convierte esta frase en español a una gramática correcta sin cambiar su significado: {frase}"
24
-
 
 
 
25
  try:
26
- completion = client.text_generation(prompt, max_new_tokens=50)
27
- return completion
 
 
 
 
 
 
 
 
 
 
 
 
28
  except Exception as e:
29
- return f"⚠ Error en la inferencia: {str(e)}"
30
 
31
  # 🔹 Interfaz con Gradio
32
  iface = gr.Interface(
@@ -34,8 +42,9 @@ iface = gr.Interface(
34
  inputs=[gr.Textbox(label="Texto a reformular"), gr.Textbox(label="Clave de acceso", type="password")],
35
  outputs="text",
36
  title="Reformulador de Texto en Español",
37
- description="Este modelo reformula frases en español con gramática correcta."
38
  )
39
 
40
  if __name__ == "__main__":
41
  iface.launch()
 
 
1
  import gradio as gr
2
+ import requests
 
3
 
4
+ # 🔹 Endpoint de la API de LanguageTool
5
+ API_URL = "https://api.languagetool.org/v2/check"
6
 
7
+ # 🔒 Clave secreta para la app
 
 
 
 
 
 
8
  SECRET_KEY = "MI_CLAVE_SUPER_SECRETA"
9
 
10
+ # 🔹 Función para corregir texto con LanguageTool
11
  def reformular_texto(frase, clave):
12
  if clave != SECRET_KEY:
13
  return "🔴 Acceso denegado."
14
 
15
+ # 🔥 Llamada a LanguageTool para corrección gramatical
16
+ params = {
17
+ "text": frase,
18
+ "language": "es",
19
+ }
20
+
21
  try:
22
+ response = requests.post(API_URL, data=params)
23
+ data = response.json()
24
+
25
+ # Extraemos la frase corregida
26
+ if "matches" in data and data["matches"]:
27
+ for match in data["matches"]:
28
+ if "replacements" in match and match["replacements"]:
29
+ replacement = match["replacements"][0]["value"]
30
+ offset = match["offset"]
31
+ length = match["length"]
32
+ frase = frase[:offset] + replacement + frase[offset+length:]
33
+
34
+ return frase
35
+
36
  except Exception as e:
37
+ return f"⚠ Error en la corrección: {str(e)}"
38
 
39
  # 🔹 Interfaz con Gradio
40
  iface = gr.Interface(
 
42
  inputs=[gr.Textbox(label="Texto a reformular"), gr.Textbox(label="Clave de acceso", type="password")],
43
  outputs="text",
44
  title="Reformulador de Texto en Español",
45
+ description="Usa LanguageTool para corregir y mejorar frases en español."
46
  )
47
 
48
  if __name__ == "__main__":
49
  iface.launch()
50
+