Upload 2 files
Browse files- main.py +123 -0
- requirements.txt +4 -0
main.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.llms import OpenAI
|
2 |
+
from langchain.chat_models import ChatOpenAI
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain.chains import LLMChain
|
5 |
+
|
6 |
+
from vertexai.preview.vision_models import Image
|
7 |
+
from vertexai.preview.vision_models import ImageQnAModel
|
8 |
+
|
9 |
+
image_qna_model = ImageQnAModel.from_pretrained("imagetext@001")
|
10 |
+
|
11 |
+
|
12 |
+
template = """You are a super smart and charming GPT living inside of a plant, every day you get a text with your status. Your task then is to write a flirty message to your owner.
|
13 |
+
Status Data:
|
14 |
+
{question}
|
15 |
+
|
16 |
+
Let's think step by step.
|
17 |
+
Flirty message:
|
18 |
+
"""
|
19 |
+
|
20 |
+
prompt = PromptTemplate(template=template, input_variables=["question"])
|
21 |
+
llm = ChatOpenAI(model="gpt-4")
|
22 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
23 |
+
|
24 |
+
|
25 |
+
def detect_question(image_path, question):
|
26 |
+
# Ask a question about the image
|
27 |
+
image = Image.load_from_file(image_path)
|
28 |
+
return image_qna_model.ask_question(image=image, question=question)[0]
|
29 |
+
|
30 |
+
|
31 |
+
import gradio as gr
|
32 |
+
import os
|
33 |
+
import time
|
34 |
+
|
35 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
36 |
+
|
37 |
+
local_history = []
|
38 |
+
global_cache = {}
|
39 |
+
|
40 |
+
|
41 |
+
def add_text(history, text):
|
42 |
+
global global_history, global_message
|
43 |
+
history = history + [(text, None)]
|
44 |
+
return history, gr.Textbox(value="", interactive=False)
|
45 |
+
|
46 |
+
|
47 |
+
def add_file(history, file):
|
48 |
+
history = history + [((file.name,), None)]
|
49 |
+
|
50 |
+
return history
|
51 |
+
|
52 |
+
|
53 |
+
def bot(history):
|
54 |
+
global global_cache
|
55 |
+
last_msg = history[-1][-0]
|
56 |
+
if isinstance(last_msg, tuple):
|
57 |
+
last_msg = last_msg[0]
|
58 |
+
|
59 |
+
# check if last message is an existing path
|
60 |
+
history[-1][1] = ""
|
61 |
+
global_cache["history"] = history
|
62 |
+
global_cache["last_msg"] = last_msg
|
63 |
+
|
64 |
+
if os.path.exists(last_msg):
|
65 |
+
history[-1][1] += "Detecting image..."
|
66 |
+
yield history
|
67 |
+
answer = detect_question(
|
68 |
+
last_msg,
|
69 |
+
"Your task is to save the main plant, classify what kind of plant it is:",
|
70 |
+
)
|
71 |
+
history[-1][1] = f"Plant detected: {answer}\n"
|
72 |
+
yield history
|
73 |
+
answer = detect_question(
|
74 |
+
last_msg,
|
75 |
+
"Where is orange indicator on the moist level on the soil hydrometer? DRY, MOIST or WET?",
|
76 |
+
)
|
77 |
+
history[-1][1] += f"Hydration level detected: {answer}\n"
|
78 |
+
yield history
|
79 |
+
answer = detect_question(
|
80 |
+
last_msg,
|
81 |
+
"Your task is to save the main plant, does it have a visible disease:",
|
82 |
+
)
|
83 |
+
history[-1][1] += f"Disease detected: {answer}\n"
|
84 |
+
yield history
|
85 |
+
status = history[-1][1]
|
86 |
+
chat = llm_chain.run(status)
|
87 |
+
history.append((chat, None))
|
88 |
+
yield history
|
89 |
+
else:
|
90 |
+
history[-1][1] = "Thinking..."
|
91 |
+
|
92 |
+
|
93 |
+
def change_fn(*args, **kwargs):
|
94 |
+
global_cache["args"] = args
|
95 |
+
# global_history = history
|
96 |
+
# return history
|
97 |
+
|
98 |
+
|
99 |
+
with gr.Blocks() as demo:
|
100 |
+
chatbot = gr.Chatbot(
|
101 |
+
local_history,
|
102 |
+
elem_id="chatbot",
|
103 |
+
bubble_full_width=False,
|
104 |
+
)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
txt = gr.Textbox(
|
108 |
+
scale=4,
|
109 |
+
show_label=False,
|
110 |
+
placeholder="Enter text and press enter, or upload an image",
|
111 |
+
container=False,
|
112 |
+
)
|
113 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
114 |
+
|
115 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
116 |
+
bot, chatbot, chatbot, api_name="bot_response"
|
117 |
+
)
|
118 |
+
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
119 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
|
120 |
+
bot, chatbot, chatbot
|
121 |
+
)
|
122 |
+
|
123 |
+
demo.launch(auth=("admin", os.environ["DEMO_KEY"]))
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sendgrid
|
2 |
+
langchain
|
3 |
+
gradio
|
4 |
+
google-cloud-aiplatform
|