aghoraguru commited on
Commit
9799c81
·
verified ·
1 Parent(s): d927f0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -2,25 +2,31 @@
2
  import os
3
  os.system(f"pip install open-interpreter")
4
  os.system(f"pip install matplotlib")
5
-
6
  import json
7
  import gradio as gr
8
  from interpreter import interpreter
9
-
10
  import time
11
  import matplotlib
12
  matplotlib.use('Agg')
13
 
 
 
 
 
 
 
 
14
  interpreter.auto_run = True
15
  interpreter.llm.model = "gpt-4-turbo"
16
  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."
17
 
18
  def update_bot(text, chatbot):
19
- response = interpreter.chat(text,stream=True, display=False)
20
- response = json_to_markdown(response)
21
- chatbot.append((text, response))
22
  return chatbot, ""
23
 
 
24
  def new_chat():
25
  interpreter.messages = []
26
  return [], ""
@@ -52,21 +58,13 @@ def create_chat_widget():
52
  return chatblock
53
 
54
  def json_to_markdown(json_data):
55
- markdown_text = []
56
  for item in json_data:
57
- if item['role'] != 'user': # Skip user entries
58
- if item['type'] == 'message':
59
- # Check if 'content' key exists before accessing it
60
- content = item.get('content', "No content available")
61
- markdown_text.append(f"**{item['role'].capitalize()}:** \n{content}")
62
- elif item['type'] == 'code':
63
- # Assume 'content' key exists, consider adding a similar check if necessary
64
- markdown_text.append(f"```{item['format']}\n{item['content']}\n```")
65
- elif item['type'] == 'console':
66
- # Check for 'content' in console type
67
- content = item.get('content', "No content available")
68
- markdown_text.append(f"```\n{content}\n```")
69
- return "\n\n".join(markdown_text)
70
 
71
  with gr.Blocks() as demo:
72
  with gr.Tab("HEXON Chatbot Assignment"):
 
2
  import os
3
  os.system(f"pip install open-interpreter")
4
  os.system(f"pip install matplotlib")
 
5
  import json
6
  import gradio as gr
7
  from interpreter import interpreter
 
8
  import time
9
  import matplotlib
10
  matplotlib.use('Agg')
11
 
12
+
13
+ #get openaiapikey from environment variable
14
+ openaiapikey = os.environ.get('OPENAI_API_KEY')
15
+
16
+ #install open-interpreter package
17
+ os.system(f"pip install open-interpreter")
18
+
19
  interpreter.auto_run = True
20
  interpreter.llm.model = "gpt-4-turbo"
21
  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."
22
 
23
  def update_bot(text, chatbot):
24
+ response_json = interpreter.chat(text, stream=True, display=False)
25
+ formatted_response = json_to_markdown(response_json)
26
+ chatbot.append((text, formatted_response))
27
  return chatbot, ""
28
 
29
+
30
  def new_chat():
31
  interpreter.messages = []
32
  return [], ""
 
58
  return chatblock
59
 
60
  def json_to_markdown(json_data):
61
+ full_message = []
62
  for item in json_data:
63
+ if item['role'] == 'assistant' and item['type'] == 'message':
64
+ content = item.get('content', " ")
65
+ full_message.append(content) # Append the content to the list
66
+ return "**Assistant:**\n" + " ".join(full_message) # Join all parts into one string and format as a single message
67
+
 
 
 
 
 
 
 
 
68
 
69
  with gr.Blocks() as demo:
70
  with gr.Tab("HEXON Chatbot Assignment"):