|
import gradio as gr |
|
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer |
|
import torch |
|
import theme |
|
|
|
|
|
theme = theme.Theme() |
|
import pydantic |
|
import os |
|
import sys |
|
sys.path.append('../..') |
|
DEVEL = os.environ.get('DEVEL', False) |
|
|
|
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.prompt import PromptTemplate |
|
from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate |
|
from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder |
|
from langchain.document_loaders import PyPDFDirectoryLoader |
|
from langchain.pydantic_v1 import BaseModel, Field, validator |
|
from langchain.output_parsers import PydanticOutputParser |
|
from langchain_community.llms import HuggingFaceHub |
|
from langchain_community.document_loaders import WebBaseLoader |
|
from typing import List |
|
from langchain.llms import OpenAI |
|
from langchain.output_parsers import PydanticOutputParser |
|
from langchain.prompts import PromptTemplate |
|
from pydantic import BaseModel, Field |
|
import shutil |
|
|
|
title= "Green Greta" |
|
|
|
from huggingface_hub import from_pretrained_keras |
|
|
|
import tensorflow as tf |
|
from tensorflow import keras |
|
from PIL import Image |
|
|
|
|
|
model1 = from_pretrained_keras("rocioadlc/EfficientNetV2L") |
|
|
|
|
|
class_labels = ['battery', |
|
'biological', |
|
'brown-glass', |
|
'cardboard', |
|
'clothes', |
|
'green-glass', |
|
'metal', |
|
'paper', |
|
'plastic', |
|
'shoes', |
|
'trash', |
|
'white-glass'] |
|
|
|
def predict_image(image_input): |
|
|
|
image = image_input.resize((244, 224)) |
|
|
|
image_array = tf.keras.preprocessing.image.img_to_array(image) |
|
|
|
image_array /= 255.0 |
|
|
|
image_array = tf.expand_dims(image_array, 0) |
|
|
|
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 |
|
|
|
|
|
loader = WebBaseLoader(["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"]) |
|
data=loader.load() |
|
|
|
text_splitter = RecursiveCharacterTextSplitter( |
|
chunk_size=1024, |
|
chunk_overlap=150, |
|
length_function=len |
|
) |
|
docs = text_splitter.split_documents(data) |
|
|
|
embeddings = HuggingFaceEmbeddings(model_name='thenlper/gte-small') |
|
|
|
persist_directory = 'docs/chroma/' |
|
|
|
|
|
shutil.rmtree(persist_directory, ignore_errors=True) |
|
vectordb = Chroma.from_documents( |
|
documents=docs, |
|
embedding=embeddings, |
|
persist_directory=persist_directory |
|
) |
|
|
|
retriever = vectordb.as_retriever(search_kwargs={"k": 2}, search_type="mmr") |
|
|
|
class Answer(BaseModel): |
|
question: str = Field(description="the original question") |
|
answer: str = Field(description="the extracted answer") |
|
|
|
|
|
parser = PydanticOutputParser(pydantic_object=Answer) |
|
|
|
|
|
prompt = PromptTemplate( |
|
template="Answer the user query.\n{format_instructions}\n{query}\n", |
|
input_variables=["query"], |
|
partial_variables={"format_instructions": parser.get_format_instructions()}, |
|
) |
|
|
|
template = """ |
|
Your name is Greta and you are a recycling chatbot with the objective to anwer questions from user in English or Spanish / |
|
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} |
|
""" |
|
|
|
|
|
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'] |
|
|
|
|
|
answer_index = output_string.rfind('"answer":') |
|
|
|
|
|
answer_part = output_string[answer_index + len('"answer":'):].strip() |
|
|
|
|
|
quote_index = answer_part.find('"') |
|
|
|
|
|
answer_value = answer_part[quote_index + 1:answer_part.find('"', quote_index + 1)] |
|
|
|
return answer_value |
|
|
|
|
|
|
|
image_gradio_app = gr.Interface( |
|
fn=predict_image, |
|
inputs=gr.Image(label="Image", sources=['upload', 'webcam'], type="pil"), |
|
outputs=[gr.Label(label="Result")], |
|
title="<span style='color: green;'>Green Greta</span>", |
|
theme=theme |
|
) |
|
|
|
chatbot_gradio_app = gr.ChatInterface( |
|
fn=chat_interface, |
|
title="<span style='color: green;'>Green Greta</span>" |
|
) |
|
|
|
|
|
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/rocioadlc/test_4/resolve/main/front_4.jpg" alt="Banner Image" style="width: 50%; max-width: 500px; margin: 0 auto;"> |
|
<h1 style="font-size: 24px; color: "#92b96a"; 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: "#92b96a"; 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: "#92b96a"; 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: "#92b96a";">¿Cómo usarlo?</span></strong> |
|
<ul style="list-style-type: disc; text-align: justify; margin-top: 20px; padding-left: 20px;"> |
|
<li style="font-size: 16px; color: "#92b96a";"><strong><span style="color: "#92b96a";">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: "#92b96a";"><strong><span style="color: "#92b96a";">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> |
|
</div> |
|
""" |
|
banner_tab = gr.Markdown(banner_tab_content) |
|
|
|
|
|
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() |
|
|