Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,127 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
return html_content
|
| 88 |
-
|
| 89 |
-
def run_chatbot(user_input):
|
| 90 |
-
"""Procesa la entrada del usuario y genera c贸digo + previsualizaci贸n."""
|
| 91 |
-
code_output = generate_code(user_input)
|
| 92 |
-
|
| 93 |
-
# Extraer HTML, CSS y JS del c贸digo generado
|
| 94 |
-
html_code, css_code, js_code = extract_code(code_output)
|
| 95 |
-
|
| 96 |
-
# Previsualizar la aplicaci贸n
|
| 97 |
-
preview = preview_app(html_code, css_code, js_code)
|
| 98 |
-
|
| 99 |
-
return (
|
| 100 |
-
f"### HTML:\n\n```html\n{html_code}\n```",
|
| 101 |
-
f"### CSS:\n\n```css\n{css_code}\n```",
|
| 102 |
-
f"### JavaScript:\n\n```javascript\n{js_code}\n```",
|
| 103 |
-
preview
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
# Crear la interfaz con Gradio
|
| 107 |
-
with gr.Blocks() as demo:
|
| 108 |
-
gr.Markdown("# Chatbot Creador de Aplicaciones")
|
| 109 |
-
with gr.Row():
|
| 110 |
-
with gr.Column():
|
| 111 |
-
user_input = gr.Textbox(label="Descripci贸n de la aplicaci贸n (Ejemplo: 'Haz un bot贸n rojo')", lines=3)
|
| 112 |
-
generate_button = gr.Button("Generar C贸digo")
|
| 113 |
-
with gr.Column():
|
| 114 |
-
html_output = gr.Code(label="C贸digo HTML", language="html")
|
| 115 |
-
css_output = gr.Code(label="C贸digo CSS", language="css")
|
| 116 |
-
js_output = gr.Code(label="C贸digo JavaScript", language="javascript")
|
| 117 |
-
preview_output = gr.HTML(label="Previsualizaci贸n")
|
| 118 |
-
|
| 119 |
-
generate_button.click(
|
| 120 |
-
run_chatbot,
|
| 121 |
-
inputs=[user_input],
|
| 122 |
-
outputs=[html_output, css_output, js_output, preview_output]
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# Lanzar la aplicaci贸n
|
| 126 |
-
if __name__ == "__main__":
|
| 127 |
-
demo.launch()
|
|
|
|
| 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 |
+
# Carga los datos de entrenamiento
|
| 11 |
+
df = pd.read_csv('./botreformasconstrucciones.csv')
|
| 12 |
+
|
| 13 |
+
# Crea un arreglo con los contextos
|
| 14 |
+
context_data = []
|
| 15 |
+
for i in range(len(df)):
|
| 16 |
+
context = ""
|
| 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 |
+
Context: {context}
|
| 64 |
+
Question: {question}
|
| 65 |
+
Answer:""")
|
| 66 |
+
|
| 67 |
+
# Crea un objeto rag_prompt con el prompt
|
| 68 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 69 |
+
|
| 70 |
+
# Crea un objeto StrOutputParser para parsear la salida
|
| 71 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 72 |
+
|
| 73 |
+
# Crea un objeto RunnablePassthrough para ejecutar el modelo
|
| 74 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 75 |
+
|
| 76 |
+
# Crea un objeto rag_chain con el modelo y el prompt
|
| 77 |
+
rag_chain = (
|
| 78 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 79 |
+
| rag_prompt
|
| 80 |
+
| llm
|
| 81 |
+
| StrOutputParser()
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Importa la biblioteca Gradio
|
| 85 |
+
import gradio as gr
|
| 86 |
+
|
| 87 |
+
# Crea una funci贸n para procesar la entrada del usuario
|
| 88 |
+
def rag_memory_stream(message, history):
|
| 89 |
+
partial_text = ""
|
| 90 |
+
for new_text in rag_chain.stream(message):
|
| 91 |
+
partial_text += new_text
|
| 92 |
+
yield partial_text
|
| 93 |
+
|
| 94 |
+
# Crea un objeto Gradio con la funci贸n y el t铆tulo
|
| 95 |
+
examples = [
|
| 96 |
+
"驴C贸mo encargar mi web contigo?",
|
| 97 |
+
"驴Como limpiar un wordpress hackeado?",
|
| 98 |
+
"驴C贸mo importar productos en wooocommerce?"
|
| 99 |
+
]
|
| 100 |
+
description = "Aplicaci贸n de IA desarrollada por GnoscticDev para servirle :)"
|
| 101 |
+
title = "Experto en desarrollo web"
|
| 102 |
+
demo = gr.ChatInterface(fn=rag_memory_stream,
|
| 103 |
+
type="messages",
|
| 104 |
+
title=title,
|
| 105 |
+
description=description,
|
| 106 |
+
fill_height=True,
|
| 107 |
+
examples=examples,
|
| 108 |
+
theme="glass",
|
| 109 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|