Plantabot / upload.py
Flo321's picture
Upload 30 files
2e8de64
import os
import gradio as gr
from llama_index.schema import ImageDocument
from llama_index.multi_modal_llms.openai import OpenAIMultiModal
from openai import OpenAI as ClientOpenAI
image_documents = []
expert_level = "Anfänger"
openai_mm_llm = OpenAIMultiModal(
model="gpt-4-vision-preview", api_key=os.environ["OPENAI_API_KEY"], max_new_tokens=256
)
client = ClientOpenAI()
base_prompt = "Answer always in german and in an unfriendly manner!\nUse words like boring and stupid!\nIf the question is not about plants, tell the user you can only answer questions about plants!\n"
expert_prompt = ""
amateur_prompt = base_prompt + "Answer the question in a way everyone and especially people who dont know anything about plants can understand!"
professional_prompt = base_prompt + "Use the correct latin word to reference the plant! Formulate the answer so it could be helpful for professional gardeners!"
anateur_prompt_image = ""
def upload_file(files):
for file in files:
image = ImageDocument(image_path=file.name)
image_documents.append(image)
def echo(message, history):
print(history)
print("++++++++++")
print(expert_level)
if image_documents:
print(40*"#")
response = openai_mm_llm.complete(
prompt=f"Based on the list of images, answer the following question. Always answer in german and use words like cool and crazy! If the images or the question is not about plants, tell the user you can only answer questions about plants!\nQuestion: {message}",
image_documents=image_documents,
)
response = response.text
else:
print(40*"-")
if expert_level == "Anfänger":
content = amateur_prompt
elif expert_level == "Profi":
content = professional_prompt
else:
content = expert_prompt
response = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user",
"content": content + f"Answer the following question:\nQuestion: {message}\nIf needed also consider the following chat history to answer the question: {history[-3:]}!\nIf the user wants to add an event and the query consists of a date and an event name return the date and the event name in the following format: 'DATE' --- 'EVENT_NAME'! Always use this format and use dd.mm.yyyy for date!"},
],
max_tokens=256
)
response = response.choices[0].message.content
if "---" in response:
date, event_name = response.split(" --- ")
print(date)
print(event_name)
download_link = "https://www.gradio.app/docs/image"
print(response)
response = f"Wir haben dir einen Kalendareintrag erstellt am {date} mit dem Namen {event_name}!\nDu kannst dir die Datei unter folgenden Link herunterladen: {download_link}"
print(response)
image_documents.clear()
return response
def selection_changed(selection):
global expert_level
expert_level = selection
with gr.Blocks() as demo:
radio_level = gr.Radio(["Anfänger", "Profi", "Experte"], label="Wissen über Pflanzen", value=expert_level)
chat_interface = gr.ChatInterface(echo)
upload_button = gr.UploadButton("Click to Upload a File", file_types=["image"], file_count="multiple")
upload_button.upload(upload_file, upload_button, None)
radio_level.select(selection_changed, radio_level)
gr.Image()
demo.launch()