Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from interpreter import interpreter
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
import matplotlib
|
| 7 |
+
matplotlib.use('Agg')
|
| 8 |
+
|
| 9 |
+
interpreter.auto_run = True
|
| 10 |
+
interpreter.llm.model = "gpt-4-turbo"
|
| 11 |
+
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."
|
| 12 |
+
|
| 13 |
+
def update_bot(text, chatbot):
|
| 14 |
+
response = interpreter.chat(text,stream=True, display=False)
|
| 15 |
+
response = json_to_markdown(response)
|
| 16 |
+
chatbot.append((text, response))
|
| 17 |
+
return chatbot, ""
|
| 18 |
+
|
| 19 |
+
def new_chat():
|
| 20 |
+
interpreter.messages = []
|
| 21 |
+
return [], ""
|
| 22 |
+
def create_chat_widget():
|
| 23 |
+
with gr.Blocks() as chatblock:
|
| 24 |
+
chatbot = gr.Chatbot(
|
| 25 |
+
[],
|
| 26 |
+
elem_id="chatbot",
|
| 27 |
+
elem_classes="chatbot",
|
| 28 |
+
layout="llm",
|
| 29 |
+
bubble_full_width=False,
|
| 30 |
+
height=600,
|
| 31 |
+
)
|
| 32 |
+
txt = gr.Textbox(
|
| 33 |
+
scale=4,
|
| 34 |
+
show_label=False,
|
| 35 |
+
placeholder="Enter text and press enter to chat with the bot.",
|
| 36 |
+
container=False,
|
| 37 |
+
)
|
| 38 |
+
new_chat_button = gr.Button(
|
| 39 |
+
"New Chat",
|
| 40 |
+
scale=3,
|
| 41 |
+
interactive=True,
|
| 42 |
+
)
|
| 43 |
+
new_chat_button.click(new_chat, [], [chatbot, txt])
|
| 44 |
+
|
| 45 |
+
txt.submit(update_bot, [txt, chatbot], [chatbot, txt])
|
| 46 |
+
|
| 47 |
+
return chatblock
|
| 48 |
+
|
| 49 |
+
def json_to_markdown(json_data):
|
| 50 |
+
return "\n\n".join(
|
| 51 |
+
f"**{item['role'].capitalize()}:** \n{item['content']}" if item['type'] == 'message' else
|
| 52 |
+
f"```{item['format']}\n{item['content']}\n```" if item['type'] == 'code' else
|
| 53 |
+
f"```\n{item['content']}\n```" for item in json_data if item['role'] != 'user'
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
with gr.Tab("HEXON Chatbot Assignment"):
|
| 58 |
+
chat_interface = create_chat_widget()
|
| 59 |
+
demo.launch()
|