Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import json
|
| 4 |
-
import requests
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Function to handle predictions
|
| 10 |
def predict(inputs, top_p, temperature, openai_api_key, system_prompt, chat_counter, chatbot=[], history=[]):
|
|
@@ -32,31 +33,30 @@ def predict(inputs, top_p, temperature, openai_api_key, system_prompt, chat_coun
|
|
| 32 |
"presence_penalty": 0,
|
| 33 |
"frequency_penalty": 0,
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"Content-Type": "application/json",
|
| 38 |
-
"Authorization": f"Bearer {openai_api_key}"
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
chat_counter += 1
|
| 42 |
history.append(inputs)
|
| 43 |
-
|
| 44 |
-
#
|
| 45 |
-
response =
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
token_counter = 0
|
| 48 |
partial_words = ""
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if chunk.decode():
|
| 57 |
-
chunk = chunk.decode()
|
| 58 |
-
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
| 59 |
-
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
| 60 |
if token_counter == 0:
|
| 61 |
history.append(" " + partial_words)
|
| 62 |
else:
|
|
@@ -70,9 +70,9 @@ def reset_textbox():
|
|
| 70 |
return gr.update(value='')
|
| 71 |
|
| 72 |
# UI Components
|
| 73 |
-
title = """<h1 align="center">
|
| 74 |
description = """
|
| 75 |
-
Explore the
|
| 76 |
"""
|
| 77 |
|
| 78 |
with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
|
@@ -84,6 +84,7 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
|
|
| 84 |
system_prompt = gr.Textbox(placeholder="Enter system prompt (optional)", label="System Prompt", lines=2)
|
| 85 |
chatbot = gr.Chatbot(elem_id='chatbot')
|
| 86 |
inputs = gr.Textbox(placeholder="Type your message here!", label="Input", lines=1)
|
|
|
|
| 87 |
state = gr.State([])
|
| 88 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 89 |
reset_btn = gr.Button("Reset Chat")
|
|
@@ -93,8 +94,8 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
|
|
| 93 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (Nucleus Sampling)")
|
| 94 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
|
| 95 |
|
| 96 |
-
# Submit input for model prediction
|
| 97 |
-
|
| 98 |
[chatbot, state, chat_counter])
|
| 99 |
reset_btn.click(reset_textbox, [], [inputs])
|
| 100 |
inputs.submit(reset_textbox, [], [inputs])
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Initialize the OpenAI client with your proxy API
|
| 5 |
+
client = openai.OpenAI(
|
| 6 |
+
api_key= openai_api_key,
|
| 7 |
+
base_url="https://api.pumpkinaigc.online/v1"
|
| 8 |
+
)
|
| 9 |
|
| 10 |
# Function to handle predictions
|
| 11 |
def predict(inputs, top_p, temperature, openai_api_key, system_prompt, chat_counter, chatbot=[], history=[]):
|
|
|
|
| 33 |
"presence_penalty": 0,
|
| 34 |
"frequency_penalty": 0,
|
| 35 |
}
|
| 36 |
+
|
| 37 |
+
# Set the chat counter
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
chat_counter += 1
|
| 39 |
history.append(inputs)
|
| 40 |
+
|
| 41 |
+
# Using the proxy API to get the response
|
| 42 |
+
response = client.Completions.create(
|
| 43 |
+
model=payload["model"],
|
| 44 |
+
messages=payload["messages"],
|
| 45 |
+
temperature=payload["temperature"],
|
| 46 |
+
top_p=payload["top_p"],
|
| 47 |
+
stream=payload["stream"],
|
| 48 |
+
presence_penalty=payload["presence_penalty"],
|
| 49 |
+
frequency_penalty=payload["frequency_penalty"]
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
token_counter = 0
|
| 53 |
partial_words = ""
|
| 54 |
|
| 55 |
+
for chunk in response:
|
| 56 |
+
if 'choices' in chunk:
|
| 57 |
+
delta = chunk['choices'][0]['delta']
|
| 58 |
+
if 'content' in delta:
|
| 59 |
+
partial_words += delta['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
if token_counter == 0:
|
| 61 |
history.append(" " + partial_words)
|
| 62 |
else:
|
|
|
|
| 70 |
return gr.update(value='')
|
| 71 |
|
| 72 |
# UI Components
|
| 73 |
+
title = """<h1 align="center">Customizable Chatbot with OpenAI API</h1>"""
|
| 74 |
description = """
|
| 75 |
+
Explore the outputs of a GPT-3.5 model, with the ability to customize system prompts, enter your OpenAI API key, and interact with a history of conversation logs.
|
| 76 |
"""
|
| 77 |
|
| 78 |
with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
|
|
|
| 84 |
system_prompt = gr.Textbox(placeholder="Enter system prompt (optional)", label="System Prompt", lines=2)
|
| 85 |
chatbot = gr.Chatbot(elem_id='chatbot')
|
| 86 |
inputs = gr.Textbox(placeholder="Type your message here!", label="Input", lines=1)
|
| 87 |
+
send_btn = gr.Button("Send")
|
| 88 |
state = gr.State([])
|
| 89 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
| 90 |
reset_btn = gr.Button("Reset Chat")
|
|
|
|
| 94 |
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (Nucleus Sampling)")
|
| 95 |
temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
|
| 96 |
|
| 97 |
+
# Submit input for model prediction with the send button
|
| 98 |
+
send_btn.click(predict, [inputs, top_p, temperature, openai_api_key, system_prompt, chat_counter, chatbot, state],
|
| 99 |
[chatbot, state, chat_counter])
|
| 100 |
reset_btn.click(reset_textbox, [], [inputs])
|
| 101 |
inputs.submit(reset_textbox, [], [inputs])
|