Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,102 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
API_URL = "https://api.openai.com/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Function to handle predictions
|
| 10 |
+
def predict(inputs, top_p, temperature, openai_api_key, system_prompt, chat_counter, chatbot=[], history=[]):
|
| 11 |
+
# Build the system prompt if provided
|
| 12 |
+
messages = []
|
| 13 |
+
if system_prompt:
|
| 14 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 15 |
+
|
| 16 |
+
# Add previous conversation history
|
| 17 |
+
if chat_counter != 0:
|
| 18 |
+
for data in chatbot:
|
| 19 |
+
messages.append({"role": "user", "content": data[0]})
|
| 20 |
+
messages.append({"role": "assistant", "content": data[1]})
|
| 21 |
+
|
| 22 |
+
# Add the current user input to the messages
|
| 23 |
+
messages.append({"role": "user", "content": inputs})
|
| 24 |
+
|
| 25 |
+
payload = {
|
| 26 |
+
"model": "gpt-3.5-turbo",
|
| 27 |
+
"messages": messages,
|
| 28 |
+
"temperature": temperature,
|
| 29 |
+
"top_p": top_p,
|
| 30 |
+
"n": 1,
|
| 31 |
+
"stream": True,
|
| 32 |
+
"presence_penalty": 0,
|
| 33 |
+
"frequency_penalty": 0,
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
headers = {
|
| 37 |
+
"Content-Type": "application/json",
|
| 38 |
+
"Authorization": f"Bearer {openai_api_key}"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
chat_counter += 1
|
| 42 |
+
history.append(inputs)
|
| 43 |
+
|
| 44 |
+
# Make a POST request to the API endpoint using the requests.post method
|
| 45 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
| 46 |
+
|
| 47 |
+
token_counter = 0
|
| 48 |
+
partial_words = ""
|
| 49 |
+
|
| 50 |
+
counter = 0
|
| 51 |
+
for chunk in response.iter_lines():
|
| 52 |
+
if counter == 0:
|
| 53 |
+
counter += 1
|
| 54 |
+
continue
|
| 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:
|
| 63 |
+
history[-1] = partial_words
|
| 64 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
| 65 |
+
token_counter += 1
|
| 66 |
+
yield chat, history, chat_counter
|
| 67 |
|
| 68 |
+
# Function to reset the textbox
|
| 69 |
+
def reset_textbox():
|
| 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;}
|
| 79 |
+
#chatbot {height: 520px; overflow: auto;}""") as demo:
|
| 80 |
+
|
| 81 |
+
gr.HTML(title)
|
| 82 |
+
with gr.Column(elem_id="col_container"):
|
| 83 |
+
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
| 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")
|
| 90 |
+
|
| 91 |
+
# Input parameters for OpenAI API
|
| 92 |
+
with gr.Accordion("Model Parameters", open=False):
|
| 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 |
+
inputs.submit(predict, [inputs, top_p, temperature, openai_api_key, system_prompt, chat_counter, chatbot, state],
|
| 98 |
+
[chatbot, state, chat_counter])
|
| 99 |
+
reset_btn.click(reset_textbox, [], [inputs])
|
| 100 |
+
inputs.submit(reset_textbox, [], [inputs])
|
| 101 |
|
| 102 |
+
demo.queue().launch(debug=True)
|
|
|