Spaces:
Runtime error
Runtime error
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 update_bot(text, chatbot): | |
response_json = interpreter.chat(text, stream=True, display=False) | |
formatted_response, images = json_to_markdown(response_json) | |
message = " ".join(formatted_response) | |
if message: | |
chatbot.append(("Assistant", message)) | |
for img_path in images: | |
chatbot.append(("Assistant", img_path)) # Append image paths directly | |
return chatbot, "" | |
def new_chat(): | |
interpreter.messages = [] | |
return [], "" | |
def create_chat_widget(): | |
with gr.Blocks() as chatblock: | |
chatbot = gr.Chatbot( | |
[], | |
elem_id="chatbot", | |
elem_classes="chatbot", | |
layout="llm", | |
bubble_full_width=False, | |
height=600, | |
) | |
txt = gr.Textbox( | |
scale=4, | |
show_label=False, | |
placeholder="Enter text and press enter to chat with the bot.", | |
container=False, | |
) | |
new_chat_button = gr.Button( | |
"New Chat", | |
scale=3, | |
interactive=True, | |
) | |
new_chat_button.click(new_chat, [], [chatbot, txt]) | |
txt.submit(update_bot, [txt, chatbot], [chatbot, txt]) | |
return chatblock | |
def json_to_markdown(json_data): | |
full_message = [] | |
images = [] | |
# Regex to detect URLs; this is a simple version and might need to be adapted | |
url_pattern = r'(http[s]?://\S+|sandbox:/\S+)' | |
for item in json_data: | |
if item['role'] == 'assistant' and item['type'] == 'message': | |
content = item.get('content', " ") | |
# Find all URLs in the content | |
urls = re.findall(url_pattern, content) | |
# Append any detected URLs to the images list | |
images.extend(urls) | |
# Remove URLs from the content | |
content = re.sub(url_pattern, "", content).strip() | |
if content: | |
full_message.append(content) | |
return full_message, images | |
with gr.Blocks() as demo: | |
with gr.Tab("HEXON Chatbot Assignment"): | |
chat_interface = create_chat_widget() | |
demo.launch() |