File size: 2,777 Bytes
4c6d6af
 
 
 
168b9a8
 
 
 
 
 
 
9799c81
 
 
 
 
 
 
168b9a8
 
 
 
 
9799c81
77a601c
 
 
 
 
 
168b9a8
 
9799c81
168b9a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9799c81
77a601c
 
 
 
d927f0b
9799c81
 
77a601c
 
 
 
 
 
 
 
 
 
9799c81
168b9a8
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

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()