Spaces:
Runtime error
Runtime error
Commit
·
d863fc7
1
Parent(s):
ade71a8
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,29 @@
|
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
|
|
|
|
|
|
|
4 |
# Define the chat function
|
5 |
def chat(api_key, model, message):
|
6 |
# Check if an API key has been provided
|
7 |
if api_key is None:
|
8 |
return "Please enter your OpenAI API key and try again."
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Set up the OpenAI API request
|
11 |
response = openai.Completion.create(
|
12 |
engine=model,
|
13 |
-
prompt=
|
14 |
max_tokens=1024,
|
15 |
n=1,
|
16 |
stop=None,
|
@@ -21,7 +34,20 @@ def chat(api_key, model, message):
|
|
21 |
# Extract the bot's response from the API request
|
22 |
bot_response = response.choices[0].text.strip()
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Define the Gradio interface
|
27 |
api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None)
|
@@ -31,7 +57,7 @@ model_input = gr.inputs.Dropdown(
|
|
31 |
default="text-davinci-003",
|
32 |
)
|
33 |
message_input = gr.inputs.Textbox(label="Enter your message here")
|
34 |
-
output = gr.outputs.
|
35 |
|
36 |
chat_button = gr.Interface(
|
37 |
fn=chat,
|
@@ -39,7 +65,7 @@ chat_button = gr.Interface(
|
|
39 |
outputs=output,
|
40 |
title="OpenAI Chatbot",
|
41 |
description="Enter your message below to chat with an AI",
|
42 |
-
theme="
|
43 |
layout="vertical",
|
44 |
allow_flagging=False,
|
45 |
allow_screenshot=False,
|
|
|
1 |
+
import re
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Define a regular expression to match Python code blocks
|
6 |
+
code_pattern = re.compile(r"```python\n(.*?)\n```", re.DOTALL)
|
7 |
+
|
8 |
# Define the chat function
|
9 |
def chat(api_key, model, message):
|
10 |
# Check if an API key has been provided
|
11 |
if api_key is None:
|
12 |
return "Please enter your OpenAI API key and try again."
|
13 |
|
14 |
+
# Extract code blocks from the message
|
15 |
+
code_blocks = extract_code_blocks(message)
|
16 |
+
|
17 |
+
# Replace code blocks with highlighted versions
|
18 |
+
highlighted_message = message
|
19 |
+
for code in code_blocks:
|
20 |
+
highlighted_code = f'<span style="background-color: #FFFF00;">{code}</span>'
|
21 |
+
highlighted_message = highlighted_message.replace(f'```python\n{code}\n```', highlighted_code)
|
22 |
+
|
23 |
# Set up the OpenAI API request
|
24 |
response = openai.Completion.create(
|
25 |
engine=model,
|
26 |
+
prompt=highlighted_message,
|
27 |
max_tokens=1024,
|
28 |
n=1,
|
29 |
stop=None,
|
|
|
34 |
# Extract the bot's response from the API request
|
35 |
bot_response = response.choices[0].text.strip()
|
36 |
|
37 |
+
# Highlight code blocks in the bot response
|
38 |
+
highlighted_bot_response = bot_response
|
39 |
+
for code in code_blocks:
|
40 |
+
highlighted_code = f'<span style="background-color: #FFFF00;">{code}</span>'
|
41 |
+
highlighted_bot_response = highlighted_bot_response.replace(code, highlighted_code)
|
42 |
+
|
43 |
+
return highlighted_bot_response
|
44 |
+
|
45 |
+
# Define a function to extract code blocks from a string
|
46 |
+
def extract_code_blocks(text):
|
47 |
+
code_blocks = []
|
48 |
+
for match in code_pattern.finditer(text):
|
49 |
+
code_blocks.append(match.group(1))
|
50 |
+
return code_blocks
|
51 |
|
52 |
# Define the Gradio interface
|
53 |
api_key_input = gr.inputs.Textbox(label="OpenAI API Key", default=None)
|
|
|
57 |
default="text-davinci-003",
|
58 |
)
|
59 |
message_input = gr.inputs.Textbox(label="Enter your message here")
|
60 |
+
output = gr.outputs.HTML(label="Bot response")
|
61 |
|
62 |
chat_button = gr.Interface(
|
63 |
fn=chat,
|
|
|
65 |
outputs=output,
|
66 |
title="OpenAI Chatbot",
|
67 |
description="Enter your message below to chat with an AI",
|
68 |
+
theme="compact",
|
69 |
layout="vertical",
|
70 |
allow_flagging=False,
|
71 |
allow_screenshot=False,
|