rocioadlc commited on
Commit
52836f6
Β·
verified Β·
1 Parent(s): 8df888c

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile.txt +11 -0
  2. app.py +170 -0
  3. requirements.txt +11 -0
  4. theme.py +99 -0
Dockerfile.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[ ]:
5
+
6
+
7
+ import gradio as gr
8
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
9
+ import torch
10
+ import theme
11
+
12
+ theme = theme.Theme()
13
+
14
+ import os
15
+ import sys
16
+ sys.path.append('../..')
17
+
18
+ #langchain
19
+ from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
20
+ from langchain.embeddings import HuggingFaceEmbeddings
21
+ from langchain.prompts import PromptTemplate
22
+ from langchain.chains import RetrievalQA
23
+ from langchain.prompts import ChatPromptTemplate
24
+ from langchain.schema import StrOutputParser
25
+ from langchain.schema.runnable import Runnable
26
+ from langchain.schema.runnable.config import RunnableConfig
27
+ from langchain.chains import (
28
+ LLMChain, ConversationalRetrievalChain)
29
+ from langchain.vectorstores import Chroma
30
+ from langchain.memory import ConversationBufferMemory
31
+ from langchain.chains import LLMChain
32
+ from langchain.prompts.prompt import PromptTemplate
33
+ from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate
34
+ from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder
35
+ from langchain.document_loaders import PyPDFDirectoryLoader
36
+ from pydantic import BaseModel, Field
37
+ from langchain.output_parsers import PydanticOutputParser
38
+ from langchain_community.llms import HuggingFaceHub
39
+ from langchain_community.document_loaders import WebBaseLoader
40
+
41
+ from pydantic import BaseModel
42
+ import shutil
43
+
44
+
45
+
46
+ custom_title = "<span style='color: rgb(243, 239, 224);'>Green Greta</span>"
47
+
48
+
49
+ # Cell 1: Image Classification Model
50
+ image_pipeline = pipeline(task="image-classification", model="guillen/vit-basura-test1")
51
+
52
+ def predict_image(input_img):
53
+ predictions = image_pipeline(input_img)
54
+ return {p["label"]: p["score"] for p in predictions}
55
+
56
+ image_gradio_app = gr.Interface(
57
+ fn=predict_image,
58
+ inputs=gr.Image(label="Image", sources=['upload', 'webcam'], type="pil"),
59
+ outputs=[gr.Label(label="Result")],
60
+ title=custom_title,
61
+ theme=theme
62
+ )
63
+
64
+ 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"])
65
+ data=loader.load()
66
+ # split documents
67
+ text_splitter = RecursiveCharacterTextSplitter(
68
+ chunk_size=1024,
69
+ chunk_overlap=150,
70
+ length_function=len
71
+ )
72
+ docs = text_splitter.split_documents(data)
73
+ # define embedding
74
+ embeddings = HuggingFaceEmbeddings(model_name='thenlper/gte-small')
75
+ # create vector database from data
76
+ persist_directory = 'docs/chroma/'
77
+
78
+ # Remove old database files if any
79
+ shutil.rmtree(persist_directory, ignore_errors=True)
80
+ vectordb = Chroma.from_documents(
81
+ documents=docs,
82
+ embedding=embeddings,
83
+ persist_directory=persist_directory
84
+ )
85
+ # define retriever
86
+ retriever = vectordb.as_retriever(search_kwargs={"k": 2}, search_type="mmr")
87
+
88
+ class FinalAnswer(BaseModel):
89
+ question: str = Field(description="the original question")
90
+ answer: str = Field(description="the extracted answer")
91
+
92
+ # Assuming you have a parser for the FinalAnswer class
93
+ parser = PydanticOutputParser(pydantic_object=FinalAnswer)
94
+
95
+ template = """
96
+ Your name is Greta and you are a recycling chatbot with the objective to anwer questions from user in English or Spanish /
97
+ Use the following pieces of context to answer the question /
98
+ If the question is English answer in English /
99
+ If the question is Spanish answer in Spanish /
100
+ Do not mention the word context when you answer a question /
101
+ Answer the question fully and provide as much relevant detail as possible. Do not cut your response short /
102
+ Context: {context}
103
+ User: {question}
104
+ {format_instructions}
105
+ """
106
+
107
+ # Create the chat prompt templates
108
+ sys_prompt = SystemMessagePromptTemplate.from_template(template)
109
+ qa_prompt = ChatPromptTemplate(
110
+ messages=[
111
+ sys_prompt,
112
+ HumanMessagePromptTemplate.from_template("{question}")],
113
+ partial_variables={"format_instructions": parser.get_format_instructions()}
114
+ )
115
+ llm = HuggingFaceHub(
116
+ repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
117
+ task="text-generation",
118
+ model_kwargs={
119
+ "max_new_tokens": 2000,
120
+ "top_k": 30,
121
+ "temperature": 0.1,
122
+ "repetition_penalty": 1.03
123
+ },
124
+ )
125
+
126
+ qa_chain = ConversationalRetrievalChain.from_llm(
127
+ llm = llm,
128
+ memory = ConversationBufferMemory(llm=llm, memory_key="chat_history", input_key='question', output_key='output'),
129
+ retriever = retriever,
130
+ verbose = True,
131
+ combine_docs_chain_kwargs={'prompt': qa_prompt},
132
+ get_chat_history = lambda h : h,
133
+ rephrase_question = False,
134
+ output_key = 'output',
135
+ )
136
+
137
+ def chat_interface(question,history):
138
+ result = qa_chain.invoke({'question': question})
139
+ output_string = result['output']
140
+
141
+ # Find the index of the last occurrence of "answer": in the string
142
+ answer_index = output_string.rfind('"answer":')
143
+
144
+ # Extract the substring starting from the "answer": index
145
+ answer_part = output_string[answer_index + len('"answer":'):].strip()
146
+
147
+ # Find the next occurrence of a double quote to get the start of the answer value
148
+ quote_index = answer_part.find('"')
149
+
150
+ # Extract the answer value between double quotes
151
+ answer_value = answer_part[quote_index + 1:answer_part.find('"', quote_index + 1)]
152
+
153
+ return answer_value
154
+
155
+
156
+ chatbot_gradio_app = gr.ChatInterface(
157
+ fn=chat_interface,
158
+ title=custom_title
159
+ )
160
+
161
+ # Combine both interfaces into a single app
162
+ app = gr.TabbedInterface(
163
+ [image_gradio_app, chatbot_gradio_app],
164
+ tab_names=["Green Greta Image Classification","Green Greta Chat"],
165
+ theme=theme
166
+ )
167
+
168
+ app.queue()
169
+ app.launch()
170
+
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ transformers
2
+ torch
3
+ langchain
4
+ langchain-community
5
+ pypdf
6
+ pydantic
7
+ sentence-transformers
8
+ chromadb
9
+ gradio
10
+ beautifulsoup4
11
+ tensorflow
theme.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[ ]:
5
+
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import Iterable
10
+
11
+ from gradio.themes.base import Base
12
+ from gradio.themes.utils import colors, fonts, sizes
13
+
14
+
15
+ class Theme(Base):
16
+ def __init__(
17
+ self,
18
+ *,
19
+ primary_hue: colors.Color | str = colors.lime,
20
+ secondary_hue: colors.Color | str = colors.emerald,
21
+ neutral_hue: colors.Color | str = colors.stone,
22
+ spacing_size: sizes.Size | str = sizes.spacing_lg,
23
+ radius_size: sizes.Size | str = sizes.radius_none,
24
+ text_size: sizes.Size | str = sizes.text_md,
25
+ font: fonts.Font | str | Iterable[fonts.Font | str] = (
26
+ fonts.GoogleFont("Quicksand"),
27
+ "ui-sans-serif",
28
+ "system-ui",
29
+ "sans-serif",
30
+ ),
31
+ font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
32
+ fonts.GoogleFont("IBM Plex Mono"),
33
+ "ui-monospace",
34
+ "Consolas",
35
+ "monospace",
36
+ ),
37
+ ):
38
+ super().__init__(
39
+ primary_hue=primary_hue,
40
+ secondary_hue=secondary_hue,
41
+ neutral_hue=neutral_hue,
42
+ spacing_size=spacing_size,
43
+ radius_size=radius_size,
44
+ text_size=text_size,
45
+ font=font,
46
+ font_mono=font_mono,
47
+ )
48
+ self.name = "theme"
49
+ super().set(
50
+ # Colors
51
+ slider_color="*neutral_900",
52
+ slider_color_dark="*neutral_500",
53
+ body_text_color="rgb(18,13,5)",
54
+ block_label_text_color="rgb(243, 239, 224)",
55
+ block_title_text_color="rgb(243, 239, 224)",
56
+ body_text_color_subdued="*neutral_400",
57
+ body_background_fill='*primary_800',
58
+ background_fill_primary='*primary_600',
59
+ background_fill_primary_dark='*primary_900',
60
+ background_fill_secondary_dark='*primary_900',
61
+ block_background_fill='rgb(53,66,48)',
62
+ block_background_fill_dark="*neutral_800",
63
+ input_background_fill_dark="*neutral_700",
64
+ # Button Colors
65
+ button_primary_background_fill="rgb(53,66,48)",
66
+ button_primary_background_fill_hover='*primary_200',
67
+ button_primary_text_color='*primary_600',
68
+ button_primary_background_fill_dark="*neutral_600",
69
+ button_primary_background_fill_hover_dark="*neutral_600",
70
+ button_primary_text_color_dark="white",
71
+ button_secondary_background_fill="*button_primary_background_fill",
72
+ button_secondary_background_fill_hover="*button_primary_background_fill_hover",
73
+ button_secondary_text_color="*button_primary_text_color",
74
+ button_cancel_background_fill="*button_primary_background_fill",
75
+ button_cancel_background_fill_hover="*button_primary_background_fill_hover",
76
+ button_cancel_text_color="*button_primary_text_color",
77
+ checkbox_label_background_fill="*button_primary_background_fill",
78
+ checkbox_label_background_fill_hover="*button_primary_background_fill_hover",
79
+ checkbox_label_text_color="*button_primary_text_color",
80
+ checkbox_background_color_selected="*neutral_600",
81
+ checkbox_background_color_dark="*neutral_700",
82
+ checkbox_background_color_selected_dark="*neutral_700",
83
+ checkbox_border_color_selected_dark="*neutral_800",
84
+ # Padding
85
+ checkbox_label_padding="*spacing_md",
86
+ button_large_padding="*spacing_lg",
87
+ button_small_padding="*spacing_sm",
88
+ # Borders
89
+ block_border_width="0px",
90
+ block_border_width_dark="1px",
91
+ shadow_drop_lg="0 1px 4px 0 rgb(0 0 0 / 0.1)",
92
+ block_shadow="*shadow_drop_lg",
93
+ block_shadow_dark="none",
94
+ # Block Labels
95
+ block_title_text_weight="600",
96
+ block_label_text_weight="600",
97
+ block_label_text_size="*text_md",
98
+ )
99
+