|
import gradio as gr |
|
from transformers import GPT2LMHeadModel, GPT2Tokenizer, BartForConditionalGeneration, BartTokenizer |
|
import torch |
|
|
|
|
|
device = torch.device("cpu") |
|
|
|
|
|
print("Cargando modelo code-autocomplete-gpt2-base...") |
|
model_name = "shibing624/code-autocomplete-gpt2-base" |
|
tokenizer = GPT2Tokenizer.from_pretrained(model_name) |
|
model = GPT2LMHeadModel.from_pretrained(model_name) |
|
|
|
|
|
model.to(device) |
|
model.eval() |
|
|
|
|
|
if tokenizer.pad_token is None: |
|
tokenizer.pad_token = tokenizer.eos_token |
|
|
|
|
|
print("Cargando modelo BART para simplificaci贸n...") |
|
bart_model_name = "facebook/bart-base" |
|
bart_tokenizer = BartTokenizer.from_pretrained(bart_model_name) |
|
bart_model = BartForConditionalGeneration.from_pretrained(bart_model_name) |
|
|
|
|
|
bart_model.to(device) |
|
bart_model.eval() |
|
|
|
def autocomplete_text(input_text, max_tokens=20): |
|
""" |
|
Autocompleta el texto/c贸digo de entrada usando code-autocomplete-gpt2-base |
|
|
|
Args: |
|
input_text (str): Texto/c贸digo inicial a completar |
|
max_tokens (int): N煤mero m谩ximo de tokens a generar |
|
|
|
Returns: |
|
str: Solo la parte nueva generada (sin el input original) |
|
""" |
|
if not input_text.strip(): |
|
return "Por favor, ingresa alg煤n texto para completar." |
|
|
|
try: |
|
|
|
inputs = tokenizer.encode(input_text, return_tensors="pt", padding=True) |
|
inputs = inputs.to(device) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model.generate( |
|
inputs, |
|
max_new_tokens=max_tokens, |
|
num_return_sequences=1, |
|
temperature=0.7, |
|
do_sample=True, |
|
pad_token_id=tokenizer.eos_token_id, |
|
eos_token_id=tokenizer.eos_token_id, |
|
attention_mask=torch.ones_like(inputs) |
|
) |
|
|
|
|
|
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
new_text = generated_text[len(input_text):].strip() |
|
|
|
if not new_text: |
|
return "No se pudo generar texto adicional." |
|
|
|
return new_text |
|
|
|
except Exception as e: |
|
return f"Error al generar texto: {str(e)}" |
|
|
|
def simplify_text(input_text, max_length=150): |
|
""" |
|
Simplifica texto complejo usando BART |
|
|
|
Args: |
|
input_text (str): Texto complejo a simplificar |
|
max_length (int): Longitud m谩xima del texto simplificado |
|
|
|
Returns: |
|
str: Texto simplificado con palabras m谩s sencillas |
|
""" |
|
if not input_text.strip(): |
|
return "Por favor, ingresa alg煤n texto para simplificar." |
|
|
|
try: |
|
|
|
prompt = f"Simplify this text using easier words: {input_text}" |
|
|
|
|
|
inputs = bart_tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True) |
|
inputs = inputs.to(device) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = bart_model.generate( |
|
inputs, |
|
max_length=max_length, |
|
min_length=20, |
|
num_return_sequences=1, |
|
temperature=0.7, |
|
do_sample=True, |
|
early_stopping=True, |
|
no_repeat_ngram_size=2, |
|
pad_token_id=bart_tokenizer.pad_token_id, |
|
eos_token_id=bart_tokenizer.eos_token_id |
|
) |
|
|
|
|
|
simplified_text = bart_tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
if simplified_text.startswith("Simplify this text using easier words:"): |
|
simplified_text = simplified_text.replace("Simplify this text using easier words:", "").strip() |
|
|
|
if not simplified_text: |
|
return "No se pudo simplificar el texto." |
|
|
|
return simplified_text |
|
|
|
except Exception as e: |
|
return f"Error al simplificar texto: {str(e)}" |
|
|
|
def create_autocomplete_interface(): |
|
""" |
|
Crea la interfaz con autocompletar y simplificaci贸n dentro de gr.Blocks() |
|
""" |
|
|
|
with gr.Blocks(title="Asistente de Texto y C贸digo") as demo: |
|
|
|
gr.Markdown("# 馃 Asistente de Texto y C贸digo") |
|
gr.Markdown("Herramientas para autocompletar c贸digo y simplificar textos complejos.") |
|
|
|
with gr.Tab("Autocompletar"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
input_textbox = gr.Textbox( |
|
label="C贸digo a completar", |
|
placeholder="def fibonacci(n):", |
|
lines=5, |
|
max_lines=10 |
|
) |
|
|
|
generate_btn = gr.Button("Completar C贸digo", variant="primary") |
|
|
|
with gr.Column(): |
|
output_textbox = gr.Textbox( |
|
label="C贸digo generado", |
|
placeholder="Aqu铆 aparecer谩 la continuaci贸n del c贸digo...", |
|
lines=5, |
|
max_lines=10, |
|
interactive=False |
|
) |
|
|
|
|
|
generate_btn.click( |
|
fn=autocomplete_text, |
|
inputs=[input_textbox], |
|
outputs=[output_textbox] |
|
) |
|
|
|
|
|
input_textbox.submit( |
|
fn=autocomplete_text, |
|
inputs=[input_textbox], |
|
outputs=[output_textbox] |
|
) |
|
|
|
|
|
with gr.Tab("Simplificar Texto"): |
|
with gr.Row(): |
|
with gr.Column(): |
|
text_input = gr.Textbox( |
|
label="Texto complejo a simplificar", |
|
placeholder="Ingresa aqu铆 el texto dif铆cil de entender...", |
|
lines=6, |
|
max_lines=12 |
|
) |
|
|
|
simplify_btn = gr.Button("Simplificar Texto", variant="secondary") |
|
|
|
with gr.Column(): |
|
simplified_output = gr.Textbox( |
|
label="Texto simplificado", |
|
placeholder="Aqu铆 aparecer谩 el texto m谩s f谩cil de entender...", |
|
lines=6, |
|
max_lines=12, |
|
interactive=False |
|
) |
|
|
|
|
|
simplify_btn.click( |
|
fn=simplify_text, |
|
inputs=[text_input], |
|
outputs=[simplified_output] |
|
) |
|
|
|
|
|
text_input.submit( |
|
fn=simplify_text, |
|
inputs=[text_input], |
|
outputs=[simplified_output] |
|
) |
|
|
|
|
|
with gr.Tab("Ejemplos"): |
|
gr.Markdown(""" |
|
### Ejemplos de Autocompletado de C贸digo: |
|
|
|
**Entrada:** "def fibonacci(n):" |
|
**Salida:** "\\n if n <= 1:\\n return n\\n return fibonacci(n-1) + fibonacci(n-2)" |
|
|
|
**Entrada:** "for i in range(" |
|
**Salida:** "10):\\n print(i)" |
|
|
|
**Entrada:** "import pandas as pd\\ndf = pd.read_csv(" |
|
**Salida:** "'data.csv')\\nprint(df.head())" |
|
|
|
--- |
|
|
|
### Ejemplos de Simplificaci贸n de Texto: |
|
|
|
**Texto complejo:** "La implementaci贸n de algoritmos de machine learning requiere una comprensi贸n profunda de estructuras de datos y t茅cnicas de optimizaci贸n." |
|
**Texto simple:** "Para usar inteligencia artificial necesitas entender bien c贸mo organizar datos y mejorar programas." |
|
|
|
**Texto complejo:** "El protocolo de comunicaci贸n as铆ncrona permite la transmisi贸n de datos sin sincronizaci贸n temporal." |
|
**Texto simple:** "Este m茅todo permite enviar informaci贸n sin esperar a que termine el env铆o anterior." |
|
""") |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
print("Iniciando aplicaci贸n de asistente de texto y c贸digo...") |
|
|
|
|
|
app = create_autocomplete_interface() |
|
|
|
|
|
app.launch( |
|
share=False, |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
show_error=True, |
|
debug=False |
|
) |