File size: 1,992 Bytes
4c6d6af
 
 
 
 
168b9a8
 
 
4c6d6af
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

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

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 = interpreter.chat(text,stream=True, display=False)
    response = json_to_markdown(response)
    chatbot.append((text, response))
    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):
    return "\n\n".join(
        f"**{item['role'].capitalize()}:** \n{item['content']}" if item['type'] == 'message' else
        f"```{item['format']}\n{item['content']}\n```" if item['type'] == 'code' else
        f"```\n{item['content']}\n```" for item in json_data if item['role'] != 'user'
    )

with gr.Blocks() as demo:
    with gr.Tab("HEXON Chatbot Assignment"):
        chat_interface = create_chat_widget()
demo.launch()