ALVHB95's picture
user agent
ae0fba3
raw
history blame contribute delete
No virus
16.4 kB
import gradio as gr
import torch
import theme
theme = theme.Theme()
from huggingface_hub import from_pretrained_keras
from tensorflow.keras.applications import EfficientNetB0
import tensorflow as tf
from tensorflow import keras
from PIL import Image
from pydantic.v1 import BaseModel, Field
import shutil
import tenacity
#langchain
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA
from langchain.prompts import ChatPromptTemplate
from langchain.schema import StrOutputParser
from langchain.schema.runnable import Runnable
from langchain.schema.runnable.config import RunnableConfig
from langchain.chains import (
LLMChain, ConversationalRetrievalChain)
from langchain.vectorstores import Chroma
from langchain.memory import ConversationBufferMemory
from langchain.chains import LLMChain
from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder
from langchain.output_parsers import PydanticOutputParser
from langchain_community.llms import HuggingFaceHub
from langchain_community.document_loaders import WebBaseLoader
from fake_useragent import UserAgent
custom_title = "<span style='color: rgb(243, 239, 224);'>Green Greta</span>"
# Cell 1: Image Classification Model
model1 = from_pretrained_keras("rocioadlc/efficientnetB0_trash")
# Define class labels
class_labels = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
# Function to predict image label and score
def predict_image(input):
# Resize the image to the size expected by the model and convert to numpy array
image_array = tf.keras.preprocessing.image.img_to_array(input.resize((244, 224))) # Cambiar el orden de las dimensiones
# Normalize the image
image_array = tf.keras.applications.efficientnet.preprocess_input(image_array)
# Expand the dimensions to create a batch
image_array = tf.expand_dims(image_array, 0)
# Predict using the model
predictions = model1.predict(image_array)
category_scores = {}
for i, class_label in enumerate(class_labels):
category_scores[class_label] = predictions[0][i].item()
return category_scores
image_gradio_app = gr.Interface(
fn=predict_image,
inputs=gr.Image(label="Image", sources=['upload', 'webcam'], type="pil"),
outputs=[gr.Label(label="Result")],
title=custom_title,
theme=theme
)
# Cell 2: ChatBot Model
# Generate a random user agent
user_agent = UserAgent().random
header_template = {"User-Agent": user_agent}
# Create the loader with the headers
loader = WebBaseLoader(
web_paths=[
"https://www.epa.gov/recycle/frequent-questions-recycling",
"https://www.whitehorsedc.gov.uk/vale-of-white-horse-district-council/recycling-rubbish-and-waste/lets-get-real-about-recycling/",
"https://www.teimas.com/blog/13-preguntas-y-respuestas-sobre-la-ley-de-residuos-07-2022",
"https://www.molok.com/es/blog/gestion-de-residuos-solidos-urbanos-rsu-10-dudas-comunes",
"https://espanol.epa.gov/espanol/el-reciclaje#valelapena",
"https://espanol.epa.gov/espanol/preguntas-frecuentes-sobre-reciclado-de-plastico-y-elaboracion-de-abono-vegetal",
"https://espanol.epa.gov/espanol/consejo-del-dia-como-reciclo-mis",
"https://espanol.epa.gov/espanol/recursos-para-reciclar-dispositivos-electronicos",
"https://www.epa.gov/recycle/electronics-donation-and-recycling",
"https://reducereutilizarecicla.org/que-es-el-reciclaje/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/contenedor-amarillo/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/contenedor-azul/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/contenedor-verde/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/contenedor-marron-organico/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/contenedor-gris-restos/",
"https://reducereutilizarecicla.org/contenedores-de-reciclaje/punto-limpio/",
"https://reducereutilizarecicla.org/donde-tirar-auriculares/",
"https://reducereutilizarecicla.org/donde-tirar-sartenes/",
"https://reducereutilizarecicla.org/donde-tirar-aceite-usado/",
"https://reducereutilizarecicla.org/como-se-reciclan-los-envases-tipo-brik/",
"https://reducereutilizarecicla.org/los-envases-del-verano/",
"https://reducereutilizarecicla.org/donde-tirar-radiografias/",
"https://reducereutilizarecicla.org/envases-ecologicos/",
"https://reducereutilizarecicla.org/donde-tirar-los-restos-de-pintura/",
"https://reducereutilizarecicla.org/valorizacion-de-residuos/",
"https://reducereutilizarecicla.org/como-reciclar-pilas/",
"https://reducereutilizarecicla.org/como-reciclar-capsulas-de-cafe/",
"https://reducereutilizarecicla.org/reciclando-cd/",
"https://reducereutilizarecicla.org/donde-tirar-neumaticos/",
"https://reducereutilizarecicla.org/como-reciclar-una-canasta-de-mimbre/",
"https://reducereutilizarecicla.org/como-funciona-el-contenedor-amarillo/",
"https://reducereutilizarecicla.org/donde-se-tiran-los-vapers/",
"https://reducereutilizarecicla.org/cuanto-tarda-una-bolsa-biodegradable-en-degradarse/",
"https://reducereutilizarecicla.org/donde-se-reciclan-los-juguetes/",
"https://reducereutilizarecicla.org/objetos-que-se-pueden-reutilizar/",
"https://reducereutilizarecicla.org/la-parafina-se-puede-reutilizar/",
"https://reducereutilizarecicla.org/planta-de-reciclaje-de-papel/",
"https://reducereutilizarecicla.org/como-saber-si-un-envase-es-reciclable/",
"https://reducereutilizarecicla.org/reutilizar-vasos-de-vela/",
"https://reducereutilizarecicla.org/bolsas-frio-calor/",
"https://reducereutilizarecicla.org/reciclar-y-reutilizar-materiales-de-construccion/",
"https://reducereutilizarecicla.org/que-es-exactamente-el-pet/",
"https://reducereutilizarecicla.org/tipos-de-reciclaje/",
"https://reducereutilizarecicla.org/que-hacer-con-palets-reciclados/",
"https://reducereutilizarecicla.org/vertederos-controlados/",
"https://reducereutilizarecicla.org/donde-tirar-escombros/",
"https://reducereutilizarecicla.org/como-reciclar-los-residuos-de-ps-poliestireno/",
"https://reducereutilizarecicla.org/tirar-la-basura-sin-bolsas/",
"https://reducereutilizarecicla.org/tirar-el-palo-de-la-fregona/",
"https://reducereutilizarecicla.org/la-mejor-manera-de-reciclar-una-pala-de-padel/",
"https://reducereutilizarecicla.org/sabes-donde-tirar-las-llantas-viejas-de-un-coche/",
"https://reducereutilizarecicla.org/sabes-donde-tirar-el-arbol-de-navidad/",
"https://reducereutilizarecicla.org/clavos-tornillos-herramientas-donde-tirar-hierro/",
"https://reducereutilizarecicla.org/donde-tirar-un-secador-de-pelo-contenedor-o-punto-limpio/",
"https://reducereutilizarecicla.org/donde-tirar-electrodomesticos/",
"https://reducereutilizarecicla.org/donde-puedo-tirar-ramas-de-arboles/",
"https://reducereutilizarecicla.org/donde-tirar-escombros/",
"https://reducereutilizarecicla.org/donde-se-tira-el-muerdago-quemado/",
"https://reducereutilizarecicla.org/sandalias-caucho-reciclado-neumaticos/",
"https://reducereutilizarecicla.org/ideas-para-reciclar-aspas-de-ventilador-de-techo/",
"https://reducereutilizarecicla.org/reciclar-sacos-dormir/",
"https://reducereutilizarecicla.org/reciclar-sillas-playa/",
"https://reducereutilizarecicla.org/donde-tirar-antipolillas/",
"https://reducereutilizarecicla.org/que-hacer-con-los-juguetes-viejos/",
"https://reducereutilizarecicla.org/como-utilizar-las-mascarillas-y-el-gel-hidroalcoholico-en-la-playa/",
"https://reducereutilizarecicla.org/ideas-para-reciclar-un-ventilador-de-pie/",
"https://reducereutilizarecicla.org/donde-tirar-gasoil/",
"https://reducereutilizarecicla.org/donde-puedo-tirar-basura-electronica/",
"https://reducereutilizarecicla.org/donde-tirar-agujas/",
"https://reducereutilizarecicla.org/donde-tirar-residuos-peligrosos/",
"https://reducereutilizarecicla.org/donde-tirar-los-cables/",
"https://reducereutilizarecicla.org/donde-tirar-bicicletas/",
"https://reducereutilizarecicla.org/donde-tirar-maletas/",
"https://reducereutilizarecicla.org/como-reciclar-una-pantalla/",
"https://reducereutilizarecicla.org/donde-tirar-ropa-usada/"
],
header_template=header_template
)
data=loader.load()
# split documents
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1024,
chunk_overlap=150,
length_function=len
)
docs = text_splitter.split_documents(data)
# define embedding
embeddings = HuggingFaceEmbeddings(model_name='thenlper/gte-small')
# create vector database from data
persist_directory = 'docs/chroma/'
# Remove old database files if any
shutil.rmtree(persist_directory, ignore_errors=True)
vectordb = Chroma.from_documents(
documents=docs,
embedding=embeddings,
persist_directory=persist_directory
)
# define retriever
retriever = vectordb.as_retriever(search_kwargs={"k": 2}, search_type="mmr")
class FinalAnswer(BaseModel):
question: str = Field()
answer: str = Field()
# Assuming you have a parser for the FinalAnswer class
parser = PydanticOutputParser(pydantic_object=FinalAnswer)
template = """
Your name is Greta and you are a recycling chatbot with the objective to anwer questions from user in English or Spanish /
Has sido diseñado y creado por el Grupo 1 del Máster en Data Science & Big Data de la promoción 2023/2024 de la Universidad Complutense de Madrid. Este grupo está fromado por Rocío, María Guillermo, Alejandra, Paloma y Álvaro /
Use the following pieces of context to answer the question /
If the question is English answer in English /
If the question is Spanish answer in Spanish /
Do not mention the word context when you answer a question /
Answer the question fully and provide as much relevant detail as possible. Do not cut your response short /
Context: {context}
User: {question}
{format_instructions}
"""
# Create the chat prompt templates
sys_prompt = SystemMessagePromptTemplate.from_template(template)
qa_prompt = ChatPromptTemplate(
messages=[
sys_prompt,
HumanMessagePromptTemplate.from_template("{question}")],
partial_variables={"format_instructions": parser.get_format_instructions()}
)
llm = HuggingFaceHub(
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
task="text-generation",
model_kwargs={
"max_new_tokens": 2000,
"top_k": 30,
"temperature": 0.1,
"repetition_penalty": 1.03
},
)
qa_chain = ConversationalRetrievalChain.from_llm(
llm = llm,
memory = ConversationBufferMemory(llm=llm, memory_key="chat_history", input_key='question', output_key='output'),
retriever = retriever,
verbose = True,
combine_docs_chain_kwargs={'prompt': qa_prompt},
get_chat_history = lambda h : h,
rephrase_question = False,
output_key = 'output',
)
def chat_interface(question,history):
result = qa_chain.invoke({'question': question})
output_string = result['output']
# Find the index of the last occurrence of "answer": in the string
answer_index = output_string.rfind('"answer":')
# Extract the substring starting from the "answer": index
answer_part = output_string[answer_index + len('"answer":'):].strip()
# Find the next occurrence of a double quote to get the start of the answer value
quote_index = answer_part.find('"')
# Extract the answer value between double quotes
answer_value = answer_part[quote_index + 1:answer_part.find('"', quote_index + 1)]
return answer_value
chatbot_gradio_app = gr.ChatInterface(
fn=chat_interface,
title=custom_title
)
# Banner tab
banner_tab_content = """
<div style="background-color: #d3e3c3; text-align: center; padding: 20px; display: flex; flex-direction: column; align-items: center;">
<img src="https://huggingface.co/spaces/ALVHB95/TFM_DataScience_APP/resolve/main/front_4.jpg" alt="Banner Image" style="width: 50%; max-width: 500px; margin: 0 auto;">
<h1 style="font-size: 24px; color: #4e6339; margin-top: 20px;">¡Bienvenido a nuestro clasificador de imágenes y chatbot para un reciclaje más inteligente!♻️</h1>
<p style="font-size: 16px; color: #4e6339; text-align: justify;">¿Alguna vez te has preguntado si puedes reciclar un objeto en particular? ¿O te has sentido abrumado por la cantidad de residuos que generas y no sabes cómo manejarlos de manera más sostenible? ¡Estás en el lugar correcto!</p>
<p style="font-size: 16px; color: #4e6339; text-align: justify;">Nuestra plataforma combina la potencia de la inteligencia artificial con la comodidad de un chatbot para brindarte respuestas rápidas y precisas sobre qué objetos son reciclables y cómo hacerlo de la manera más eficiente.</p>
<p style="font-size: 16px; text-align:center;"><strong><span style="color: #4e6339;">¿Cómo usarlo?</span></strong></p>
<ul style="list-style-type: disc; text-align: justify; margin-top: 20px; padding-left: 20px;">
<li style="font-size: 16px; color: #4e6339;"><strong><span style="color: #4e6339;">Green Greta Image Classification:</span></strong> Ve a la pestaña Greta Image Classification y simplemente carga una foto del objeto que quieras reciclar, y nuestro modelo de identificará de qué se trata🕵️‍♂️ para que puedas desecharlo adecuadamente.</li>
<li style="font-size: 16px; color: #4e6339;"><strong><span style="color: #4e6339;">Green Greta Chat:</span></strong> ¿Tienes preguntas sobre reciclaje, materiales específicos o prácticas sostenibles? ¡Pregunta a nuestro chatbot en la pestaña Green Greta Chat!📝 Está aquí para responder todas tus preguntas y ayudarte a tomar decisiones más informadas sobre tu reciclaje.</li>
</ul>
<h1 style="font-size: 24px; color: #4e6339; margin-top: 20px;">Welcome to our image classifier and chatbot for smarter recycling!♻️</h1>
<p style="font-size: 16px; color: #4e6339; text-align: justify;">Have you ever wondered if you can recycle a particular object? Or felt overwhelmed by the amount of waste you generate and don't know how to handle it more sustainably? You're in the right place!</p>
<p style="font-size: 16px; color: #4e6339; text-align: justify;">Our platform combines the power of artificial intelligence with the convenience of a chatbot to provide you with quick and accurate answers about which objects are recyclable and how to do it most efficiently.</p>
<p style="font-size: 16px; text-align:center;"><strong><span style="color: #4e6339;">How to use it?</span></strong>
<ul style="list-style-type: disc; text-align: justify; margin-top: 20px; padding-left: 20px;">
<li style="font-size: 16px; color: #4e6339;"><strong><span style="color: #4e6339;">Green Greta Image Classification:</span></strong> Go to the Greta Image Classification tab and simply upload a photo of the object you want to recycle, and our model will identify what it is🕵️‍♂️ so you can dispose of it properly.</li>
<li style="font-size: 16px; color: #4e6339;"><strong><span style="color: #4e6339;">Green Greta Chat:</span></strong> Have questions about recycling, specific materials, or sustainable practices? Ask our chatbot in the Green Greta Chat tab!📝 It's here to answer all your questions and help you make more informed decisions about your recycling.</li>
</ul>
</div>
"""
banner_tab = gr.Markdown(banner_tab_content)
# Combine interfaces into a single app
app = gr.TabbedInterface(
[banner_tab, image_gradio_app, chatbot_gradio_app],
tab_names=["Welcome to Green Greta", "Green Greta Image Classification", "Green Greta Chat"],
theme=theme
)
app.queue()
app.launch()