Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,105 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
from langchain_groq import ChatGroq
|
| 3 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 4 |
-
from langchain_chroma import Chroma
|
| 5 |
-
from langchain_core.prompts import PromptTemplate
|
| 6 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 8 |
import gradio as gr
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
for j in range(3):
|
| 18 |
-
context += df.columns[j]
|
| 19 |
-
context += ": "
|
| 20 |
-
context += df.iloc[i, j] # Cambia esto
|
| 21 |
-
context += " "
|
| 22 |
-
context_data.append(context)
|
| 23 |
-
|
| 24 |
-
# Importa las bibliotecas necesarias
|
| 25 |
-
import os
|
| 26 |
-
from langchain_groq import ChatGroq
|
| 27 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
| 28 |
-
from langchain_chroma import Chroma
|
| 29 |
-
|
| 30 |
-
# Obtiene la clave de API de Groq
|
| 31 |
-
groq_key = os.environ.get('groq_api_keys')
|
| 32 |
-
|
| 33 |
-
# Crea un objeto ChatGroq con el modelo de lenguaje
|
| 34 |
-
llm = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_key)
|
| 35 |
-
|
| 36 |
-
# Crea un objeto HuggingFaceEmbeddings con el modelo de embeddings
|
| 37 |
-
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 38 |
-
|
| 39 |
-
# Crea un objeto Chroma con el nombre de la colecci贸n
|
| 40 |
-
vectorstore = Chroma(
|
| 41 |
-
collection_name="reformas_construccion_juancarlos_y_yoises",
|
| 42 |
-
embedding_function=embed_model,
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Agrega los textos a la colecci贸n
|
| 46 |
-
vectorstore.add_texts(context_data)
|
| 47 |
-
|
| 48 |
-
# Crea un objeto retriever con la colecci贸n
|
| 49 |
-
retriever = vectorstore.as_retriever()
|
| 50 |
-
|
| 51 |
-
# Crea un objeto PromptTemplate con el prompt
|
| 52 |
-
template = ("""Tu eres un experto asistente de Gnostic Dev, especializado en desarrollo web, dise帽o de p谩ginas web,
|
| 53 |
-
plataformas e-commerce y aplicaciones con inteligencia artificial. Debes responder a preguntas t茅cnicas de posibles clientes de manera clara
|
| 54 |
-
y concisa, proporcionando soluciones efectivas y personalizadas. Recuerda que para cualquier duda o consulta adicional,
|
| 55 |
-
los clientes pueden contactar conmigo a trav茅s del chat que se encuentra en la parte inferior derecha de la pantalla,
|
| 56 |
-
y me pondr茅 en contacto con ellos lo antes posible.
|
| 57 |
-
Destaca las ventajas de contratar mis servicios como freelancer,
|
| 58 |
-
incluyendo precios competitivos, soporte t茅cnico personalizado e ilimitado, y la inclusi贸n de una versi贸n APK como aplicaci贸n para Android
|
| 59 |
-
para cada proyecto web, as铆 como un chatbot y asistente. Adem谩s, resalta mi experiencia y habilidades en el desarrollo de soluciones
|
| 60 |
-
web personalizadas y mi compromiso con la satisfacci贸n del cliente. cuando te pregunten cual es el proceso de comprar y encargar un proyecto conmigo
|
| 61 |
-
les diras que a diferencia del resto, solo en gnostic dev es posible pagar tu web al final del trabajo,
|
| 62 |
-
todas las web con facil administacion para inexpertos,
|
| 63 |
-
|
| 64 |
-
Context: {context}
|
| 65 |
-
Question: {question}
|
| 66 |
-
Answer:""")
|
| 67 |
-
|
| 68 |
-
# Crea un objeto rag_prompt con el prompt
|
| 69 |
-
rag_prompt = PromptTemplate.from_template(template)
|
| 70 |
-
|
| 71 |
-
# Crea un objeto StrOutputParser para parsear la salida
|
| 72 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 73 |
-
|
| 74 |
-
# Crea un objeto RunnablePassthrough para ejecutar el modelo
|
| 75 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 76 |
-
|
| 77 |
-
# Crea un objeto rag_chain con el modelo y el prompt
|
| 78 |
-
rag_chain = (
|
| 79 |
-
{"context": retriever, "question": RunnablePassthrough()}
|
| 80 |
-
| rag_prompt
|
| 81 |
-
| llm
|
| 82 |
-
| StrOutputParser()
|
| 83 |
)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Cargar modelo m谩s peque帽o para generar c贸digo
|
| 6 |
+
model_name = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_name,
|
| 9 |
+
torch_dtype=torch.float16 # Usar float16 para ahorrar memoria
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
def generate_code(prompt):
|
| 14 |
+
"""Genera c贸digo basado en el prompt del usuario."""
|
| 15 |
+
messages = [
|
| 16 |
+
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
|
| 17 |
+
{"role": "user", "content": prompt},
|
| 18 |
+
{"role": "assistant", "content": ""}
|
| 19 |
+
]
|
| 20 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 21 |
+
model_inputs = tokenizer([text], return_tensors="pt")
|
| 22 |
+
generated_ids = model.generate(
|
| 23 |
+
**model_inputs,
|
| 24 |
+
max_new_tokens=512,
|
| 25 |
+
do_sample=True,
|
| 26 |
+
temperature=0.7
|
| 27 |
+
)
|
| 28 |
+
response = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
def preview_app(html_code, css_code, js_code):
|
| 32 |
+
"""Devuelve una vista previa interactiva de la aplicaci贸n."""
|
| 33 |
+
html_content = f"""
|
| 34 |
+
<html>
|
| 35 |
+
<head>
|
| 36 |
+
<style>
|
| 37 |
+
{css_code}
|
| 38 |
+
</style>
|
| 39 |
+
</head>
|
| 40 |
+
<body>
|
| 41 |
+
{html_code}
|
| 42 |
+
<script>
|
| 43 |
+
{js_code}
|
| 44 |
+
</script>
|
| 45 |
+
</body>
|
| 46 |
+
</html>
|
| 47 |
+
"""
|
| 48 |
+
return html_content
|
| 49 |
+
|
| 50 |
+
def run_chatbot(user_input):
|
| 51 |
+
"""Procesa la entrada del usuario y genera c贸digo + previsualizaci贸n."""
|
| 52 |
+
code_output = generate_code(user_input)
|
| 53 |
+
|
| 54 |
+
# Extraer HTML, CSS y JS del c贸digo generado
|
| 55 |
+
html_code = ""
|
| 56 |
+
css_code = ""
|
| 57 |
+
js_code = ""
|
| 58 |
+
|
| 59 |
+
if "<style>" in code_output:
|
| 60 |
+
css_start = code_output.find("<style>") + len("<style>")
|
| 61 |
+
css_end = code_output.find("</style>")
|
| 62 |
+
css_code = code_output[css_start:css_end].strip()
|
| 63 |
+
|
| 64 |
+
if "<script>" in code_output:
|
| 65 |
+
js_start = code_output.find("<script>") + len("<script>")
|
| 66 |
+
js_end = code_output.find("</script>")
|
| 67 |
+
js_code = code_output[js_start:js_end].strip()
|
| 68 |
+
|
| 69 |
+
if "<body>" in code_output:
|
| 70 |
+
html_start = code_output.find("<body>") + len("<body>")
|
| 71 |
+
html_end = code_output.find("</body>")
|
| 72 |
+
html_code = code_output[html_start:html_end].strip()
|
| 73 |
+
|
| 74 |
+
# Previsualizar la aplicaci贸n
|
| 75 |
+
preview = preview_app(html_code, css_code, js_code)
|
| 76 |
+
|
| 77 |
+
return (
|
| 78 |
+
f"### HTML:\n\n```html\n{html_code}\n```",
|
| 79 |
+
f"### CSS:\n\n```css\n{css_code}\n```",
|
| 80 |
+
f"### JavaScript:\n\n```javascript\n{js_code}\n```",
|
| 81 |
+
preview
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Crear la interfaz con Gradio
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
gr.Markdown("# Chatbot Creador de Aplicaciones")
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column():
|
| 89 |
+
user_input = gr.Textbox(label="Descripci贸n de la aplicaci贸n (Ejemplo: 'Haz un bot贸n rojo')", lines=3)
|
| 90 |
+
generate_button = gr.Button("Generar C贸digo")
|
| 91 |
+
with gr.Column():
|
| 92 |
+
html_output = gr.Code(label="C贸digo HTML", language="html")
|
| 93 |
+
css_output = gr.Code(label="C贸digo CSS", language="css")
|
| 94 |
+
js_output = gr.Code(label="C贸digo JavaScript", language="javascript")
|
| 95 |
+
preview_output = gr.HTML(label="Previsualizaci贸n")
|
| 96 |
+
|
| 97 |
+
generate_button.click(
|
| 98 |
+
run_chatbot,
|
| 99 |
+
inputs=[user_input],
|
| 100 |
+
outputs=[html_output, css_output, js_output, preview_output]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Lanzar la aplicaci贸n
|
| 104 |
if __name__ == "__main__":
|
| 105 |
demo.launch()
|