codeinterpreter / app.py
aghoraguru's picture
Finally FInaly
9cbea96 verified
raw
history blame
4.05 kB
import re
import os
os.system(f"pip install open-interpreter")
os.system(f"pip install matplotlib")
import json
import gradio as gr
from interpreter import interpreter
import time
import matplotlib
matplotlib.use('Agg')
#get openaiapikey from environment variable
openaiapikey = os.environ.get('OPENAI_API_KEY')
#install open-interpreter package
os.system(f"pip install open-interpreter")
interpreter.auto_run = True
interpreter.llm.model = "gpt-4-turbo"
interpreter.custom_instructions = "First ask the user what they want to do. Based on the input, describe the next steps. If user agrees, proceed; if not, ask what they want next.If it is anything to display , always at the end open up the file."
def json_to_markdown(json_data):
full_message = ""
images = []
for item in json_data:
if item['role'] == 'assistant':
if item['type'] == 'message':
content = item.get('content', " ")
full_message += content + " "
elif item['type'] == 'image' and 'path' in item:
images.append(item['path'])
return full_message.strip(), images
def list_png_files(directory):
"""List .png files in a given directory, sorted by modification time."""
png_files = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.png')]
png_files.sort(key=lambda x: os.path.getmtime(x), reverse=True) # Newest first
return png_files
def update_images(image_component, directory="/home/user/app/"):
"""Update the image component with the latest image from the directory."""
png_files = list_png_files(directory)
if png_files:
# Load the most recent image file
return png_files[0]
return "No images available"
def create_chat_widget():
with gr.Blocks() as chatblock:
# Declare chatbot and txt here so they are accessible later
chatbot = gr.Chatbot(
[],
elem_id="gpt4",
elem_classes="gpt4",
layout="llm",
bubble_full_width=False,
height=600,
)
txt = gr.Textbox(
placeholder="Enter text and press enter to chat with the bot.",
show_label=False,
)
# Main chat interface row
with gr.Row():
send_button = gr.Button("Send")
txt.submit(update_bot, inputs=[txt, chatbot], outputs=[chatbot, txt])
send_button.click(update_bot, inputs=[txt, chatbot], outputs=[chatbot, txt])
# Adding a row for the New Chat button at the top
with gr.Row():
new_chat_button = gr.Button("New Chat", scale=3, interactive=True)
# Ensure new_chat function resets both chatbot and txt components
new_chat_button.click(new_chat, inputs=[], outputs=[chatbot, txt])
# Image display row
with gr.Row():
image_display = gr.Image()
update_image_button = gr.Button("Update Image")
update_image_button.click(update_images, inputs=[], outputs=image_display)
return chatblock
def new_chat():
return [], ""
def update_bot(text, chatbot):
if text: # Append user input to the chat with "User:" prefix
chatbot.append(("User", f"User: {text}"))
response_json = interpreter.chat(text, stream=True, display=False)
formatted_response, images = json_to_markdown(response_json)
# Append text response with "Assistant:" prefix if it exists
if formatted_response.strip():
chatbot.append(("Assistant", f"Assistant: {formatted_response}"))
# Append images if any
for img_path in images:
if os.path.exists(img_path) and img_path.endswith('.png'):
chatbot.append(("Assistant", img_path)) # Assuming the path is accessible
else:
chatbot.append(("Assistant", "Image not found or path is invalid."))
return chatbot, ""
with gr.Blocks() as demo:
with gr.Tab("HEXON Chatbot Assignment"):
chat_interface = create_chat_widget()
demo.launch()