alvibe75 commited on
Commit
f3f98d8
·
verified ·
1 Parent(s): d9faf92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -1,42 +1,44 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
  # 🔹 API de LanguageTool
5
- API_URL = "https://api.languagetool.org/v2/check"
 
 
 
 
 
6
 
7
  # 🔒 Clave secreta
8
  SECRET_KEY = "MI_CLAVE_SUPER_SECRETA"
9
 
10
- # 🔹 Función mejorada con un prompt más claro
11
  def reformular_texto(frase, clave):
12
  if clave != SECRET_KEY:
13
  return "🔴 Acceso denegado."
14
 
15
- # 🔥 Nueva versión del prompt con más instrucciones
16
- prompt = f"Reformula esta frase en español usando gramática correcta y expresiones naturales: {frase}"
17
-
18
- params = {
19
- "text": prompt, # Pasamos la frase con la nueva instrucción
20
- "language": "es",
21
- }
22
-
23
- try:
24
- response = requests.post(API_URL, data=params)
25
- data = response.json()
26
 
27
- # Aplicamos las correcciones
28
- if "matches" in data and data["matches"]:
29
- for match in data["matches"]:
30
- if "replacements" in match and match["replacements"]:
31
- replacement = match["replacements"][0]["value"]
32
- offset = match["offset"]
33
- length = match["length"]
34
- frase = frase[:offset] + replacement + frase[offset+length:]
35
 
36
- return frase if frase != prompt else "⚠ No se detectaron cambios."
 
 
 
 
 
37
 
38
- except Exception as e:
39
- return f"⚠ Error en la corrección: {str(e)}"
40
 
41
  # 🔹 Interfaz con Gradio
42
  iface = gr.Interface(
@@ -44,7 +46,7 @@ iface = gr.Interface(
44
  inputs=[gr.Textbox(label="Texto a reformular"), gr.Textbox(label="Clave de acceso", type="password")],
45
  outputs="text",
46
  title="Reformulador de Texto en Español",
47
- description="Usa LanguageTool para reformular frases en español con expresiones naturales."
48
  )
49
 
50
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import requests
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
  # 🔹 API de LanguageTool
6
+ LT_API_URL = "https://api.languagetool.org/v2/check"
7
+
8
+ # 🔹 Modelo de GPT-J en Hugging Face
9
+ MODEL_NAME = "EleutherAI/gpt-j-6B"
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
12
 
13
  # 🔒 Clave secreta
14
  SECRET_KEY = "MI_CLAVE_SUPER_SECRETA"
15
 
16
+ # 🔹 Función de reformulación con doble paso
17
  def reformular_texto(frase, clave):
18
  if clave != SECRET_KEY:
19
  return "🔴 Acceso denegado."
20
 
21
+ # 🔥 Paso 1: Corrección con LanguageTool
22
+ params = {"text": frase, "language": "es"}
23
+ response = requests.post(LT_API_URL, data=params)
24
+ data = response.json()
 
 
 
 
 
 
 
25
 
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
+ # Si LanguageTool no ha hecho cambios, pasamos a GPT-J
35
+ if frase == params["text"]:
36
+ prompt = f"Reformula esta frase en español con gramática correcta y expresiones naturales: {frase}"
37
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True)
38
+ outputs = model.generate(inputs.input_ids, max_length=50, temperature=0.7, top_k=50)
39
+ frase = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
 
41
+ return frase
 
42
 
43
  # 🔹 Interfaz con Gradio
44
  iface = gr.Interface(
 
46
  inputs=[gr.Textbox(label="Texto a reformular"), gr.Textbox(label="Clave de acceso", type="password")],
47
  outputs="text",
48
  title="Reformulador de Texto en Español",
49
+ description="Usa LanguageTool y GPT-J para mejorar frases en español con expresiones naturales."
50
  )
51
 
52
  if __name__ == "__main__":