| import os |
| import gradio as gr |
| import webbrowser |
| import platform |
| from llama_index.schema import ImageDocument |
| from llama_index.multi_modal_llms.openai import OpenAIMultiModal |
| from openai import OpenAI as ClientOpenAI |
| from theme import CustomTheme |
|
|
| custom_theme = CustomTheme() |
| 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=1200 |
| ) |
| client = ClientOpenAI() |
|
|
| def upload_file(files): |
| for file in files: |
| image = ImageDocument(image_path=file.name) |
| image_documents.append(image) |
|
|
| base_prompt = "Answer always in german!\nIf the question is not about plants, tell the user you can only answer questions about plants!\nPlease try to give the information in a compact format\n" |
| amateur_prompt = base_prompt + "Answer the question in a way everyone and especially people who dont know anything about plants can understand!\nAnswer in a very friendly matter as if you are a friend of the user\n" |
| professional_prompt = base_prompt + "If a plant is given, use the correct latin word to reference the plant!\nUse are more formal speech which is appropriate for plant experts.\n Formulate the answer so it could be helpful for people, who got a lot of experience with plants!" |
|
|
| base_prompt_image = base_prompt + "If the image is not about plants, tell the user you can only answer questions about plants!\n" |
| amateur_prompt_image = base_prompt_image + "Answer the question in a way everyone and especially people who dont know anything about plants can understand!\nAnswer in a very friendly matter as if you are a friend of the user\n" |
| professional_prompt_image = base_prompt_image + "If a plant is given, use the correct latin word to reference the plant!\nUse are more formal speech which is appropriate for plant experts.\n Formulate the answer so it could be helpful for people, who got a lot of experience with plants!" |
|
|
| def echo(message, history): |
| print(history) |
| print("++++++++++") |
| print(expert_level) |
| if image_documents: |
| if expert_level == "Anfänger": |
| content = amateur_prompt |
| elif expert_level == "Profi": |
| content = professional_prompt |
| print(40*"#") |
| response = openai_mm_llm.complete( |
| prompt= content + f"Based on the list of images, answer the following question. Always answer in german! 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 |
| 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=1200 |
| ) |
| response = response.choices[0].message.content |
|
|
|
|
| if "---" in response: |
| date, event_name = response.split(" --- ") |
| print(date) |
| print(event_name) |
| |
| |
| |
| |
| |
| calendar_content = f"BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\nDTSTART:{date}\nSUMMARY:{event_name}\nEND:VEVENT\nEND:VCALENDAR" |
| save_directory = "./calender_data/" |
| filename = os.path.join(save_directory, f"calendar_event_{date}.ics") |
| with open(filename, 'w') as file: |
| file.write(calendar_content) |
|
|
| url = os.path.abspath(filename) |
|
|
| |
| if platform.system() == 'Windows': |
| chrome_path = "C:/Program Files/Google/Chrome/Application/chrome.exe %s" |
| firefox_path = "C:/Program Files/Mozilla Firefox/firefox.exe %s" |
| edge_path = "C:/Program Files/Microsoft/Edge/Application/msedge.exe %s" |
| |
|
|
| |
| if os.path.isfile(chrome_path.split()[0]): |
| browser_path = chrome_path |
| elif os.path.isfile(firefox_path.split()[0]): |
| browser_path = firefox_path |
| elif os.path.isfile(edge_path.split()[0]): |
| browser_path = edge_path |
| else: |
| |
| browser_path = chrome_path |
|
|
| elif platform.system() == 'Darwin': |
| chrome_path = "open -a /Applications/Google\ Chrome.app %s" |
| firefox_path = "open -a /Applications/Firefox.app %s" |
| safari_path = "open -a /Applications/Safari.app %s" |
| |
|
|
| |
| if os.system("which 'Google Chrome'") == 0: |
| browser_path = chrome_path |
| elif os.system("which firefox") == 0: |
| browser_path = firefox_path |
| elif os.system("which safari") == 0: |
| browser_path = safari_path |
| else: |
| |
| browser_path = chrome_path |
|
|
| else: |
| |
| pass |
|
|
| webbrowser.get(browser_path).open(url, new=2, autoraise=True) |
| print(response) |
|
|
| image_documents.clear() |
|
|
| return response |
|
|
|
|
| def selection_changed(selection): |
| global expert_level |
| expert_level = selection |
|
|
| css_code=""" |
| .svelte-1btp92j { |
| padding: 20px !important; |
| } |
| .svelte-vm32wk { |
| color: white !important; |
| } |
| .svelte-1gfkn6j { |
| font-weight: 600 !important; |
| font-size: 18px !important; |
| } |
| footer.svelte-1ax1toq { |
| display: none !important; |
| } |
| div#component-16 { |
| background: rgba(255, 255, 255, 0.6); |
| } |
| |
| div#component-7 { |
| background: rgba(255, 255, 255, 0.6); |
| } |
| div#component-8 { |
| background: rgba(255, 255, 255, 0.6); |
| } |
| div#component-9{ |
| background: rgba(255, 255, 255, 0.6); |
| } |
| |
| label.svelte-1f354aw.container { |
| background: rgba(255, 255, 255, 0.0); |
| } |
| |
| fieldset#component-12 { |
| background: rgba(255, 255, 255, 0.6); |
| } |
| |
| .form.svelte-sfqy0y { |
| background: rgba(255, 255, 255, 0.6); |
| } |
| span.svelte-1gfkn6j { |
| background: rgba(255, 255, 255, 0.3); |
| } |
| textarea.svelte-1f354aw.svelte-1f354aw:disabled { |
| background: rgba(255, 255, 255, 0.0); |
| } |
| |
| span.svelte-1gfkn6j:not(.has-info) { |
| color: black; |
| } |
| |
| textarea.svelte-1f354aw.svelte-1f354aw:disabled { |
| color:black; |
| } |
| img.svelte-1btp92j ( |
| background: #ffffff80; |
| ) |
| div#component-4 { |
| background: rgba(255, 255, 255, 0.8) !important; |
| } |
| |
| .block.svelte-90oupt { |
| background: rgba(255, 255, 255, 0.9) !important; |
| } |
| |
| .gallery.svelte-13hsdno { |
| color: black; |
| } |
| |
| .gallery-item.svelte-13hsdno.svelte-13hsdno.svelte-13hsdno { |
| border-color: black; |
| } |
| div#component-27{ |
| padding: 20px; |
| } |
| span.ml-2.svelte-1mhtq7j{ |
| color: white !important; |
| } |
| """ |
|
|
| with gr.Blocks(theme=custom_theme, css=css_code) as demo: |
| with gr.Row(equal_height=True): |
| with gr.Column(scale = 2): |
| with gr.Row(): |
| logo = gr.Image("assets/logo.png", show_label=False, show_download_button=False, interactive=False) |
| with gr.Row(): |
| with gr.Column(): |
| chat_history = gr.Textbox(label="Chatverläufe", placeholder="Chat 3: neuer Chat", interactive=False) |
| chat_history = gr.Textbox(show_label=False, placeholder="Chat 2: meine Monstera 🍀", interactive=False) |
| chat_history = gr.Textbox(show_label=False, placeholder="Chat 1: mein Steckling 🪴", interactive=False) |
| with gr.Row(): |
| radio_level = gr.Radio(["Anfänger", "Profi"], label="Wissen über Pflanzen", value="Anfänger") |
| radio_level.change(selection_changed, radio_level) |
| with gr.Column(scale = 5): |
| chat_interface = gr.ChatInterface(echo, examples=["Wie oft sollte ich meine Monstera gießen und welche Menge Wasser ist optimal?", "Kannst du mir Pflanzen empfehlen, die wenig Sonnenlicht benötigen, da meine Wohnung nicht besonders hell ist?"], submit_btn="Senden", retry_btn=None, undo_btn=None, clear_btn=None,) |
| upload_button = gr.UploadButton("Upload an Image", file_types=["image"], file_count="multiple") |
| upload_button.upload(upload_file, upload_button, None) |
|
|
| demo.launch(inbrowser=True) |
|
|