Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,36 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
with gr.Blocks() as demo:
|
| 4 |
chat_history = gr.State(value=[])
|
| 5 |
-
|
| 6 |
-
def
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
chat_history.value.append(
|
| 9 |
{
|
| 10 |
"user": message,
|
|
|
|
| 11 |
"bot": response,
|
| 12 |
}
|
| 13 |
)
|
|
@@ -15,26 +38,26 @@ with gr.Blocks() as demo:
|
|
| 15 |
|
| 16 |
def generate_json(chat_history):
|
| 17 |
return chat_history.value
|
| 18 |
-
|
| 19 |
chatbox = gr.ChatInterface(
|
| 20 |
-
fn=
|
| 21 |
-
|
| 22 |
-
title="
|
| 23 |
-
description="
|
| 24 |
-
examples=["
|
| 25 |
-
|
| 26 |
-
submit_btn="
|
| 27 |
-
stop_btn="Stop
|
| 28 |
-
retry_btn="
|
| 29 |
undo_btn="Undo last message",
|
| 30 |
clear_btn="Start a new conversation"
|
| 31 |
).queue()
|
| 32 |
-
|
| 33 |
chat_history_json = gr.JSON(generate_json(chat_history))
|
| 34 |
gr.Markdown("### 📩 Generate the JSON file for your chat history!")
|
| 35 |
-
gr.Interface(fn=generate_json,
|
| 36 |
-
inputs=None,
|
| 37 |
-
outputs=[
|
| 38 |
-
|
| 39 |
demo.queue()
|
| 40 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai_secret_manager
|
| 3 |
+
|
| 4 |
+
# Function to get the secret values
|
| 5 |
+
def get_secrets():
|
| 6 |
+
secrets = openai_secret_manager.get_secret("your_api_key_secret_name")
|
| 7 |
+
instructor_prompt = openai_secret_manager.get_secret("your_instructor_prompt_secret_name")
|
| 8 |
+
return secrets, instructor_prompt
|
| 9 |
+
|
| 10 |
+
# Get the secrets
|
| 11 |
+
secrets, instructor_prompt = get_secrets()
|
| 12 |
+
|
| 13 |
+
# Set your API key
|
| 14 |
+
api_key = secrets["api_key"]
|
| 15 |
|
| 16 |
with gr.Blocks() as demo:
|
| 17 |
chat_history = gr.State(value=[])
|
| 18 |
+
|
| 19 |
+
def chat_with_instructor(message, history):
|
| 20 |
+
import openai
|
| 21 |
+
|
| 22 |
+
openai.api_key = api_key
|
| 23 |
+
chat_input = instructor_prompt + "\nUser: " + message + "\nInstructor:"
|
| 24 |
+
response = openai.Completion.create(
|
| 25 |
+
engine="text-davinci-002",
|
| 26 |
+
prompt=chat_input,
|
| 27 |
+
max_tokens=50 # Adjust this for response length
|
| 28 |
+
).choices[0].text.strip()
|
| 29 |
+
|
| 30 |
chat_history.value.append(
|
| 31 |
{
|
| 32 |
"user": message,
|
| 33 |
+
"instructor": instructor_prompt,
|
| 34 |
"bot": response,
|
| 35 |
}
|
| 36 |
)
|
|
|
|
| 38 |
|
| 39 |
def generate_json(chat_history):
|
| 40 |
return chat_history.value
|
| 41 |
+
|
| 42 |
chatbox = gr.ChatInterface(
|
| 43 |
+
fn=chat_with_instructor,
|
| 44 |
+
|
| 45 |
+
title="Chat with Instructor",
|
| 46 |
+
description="Chat with the instructor using a predefined prompt.",
|
| 47 |
+
examples=["Hi, can you help me with this?"],
|
| 48 |
+
|
| 49 |
+
submit_btn="Send",
|
| 50 |
+
stop_btn="Stop",
|
| 51 |
+
retry_btn="Retry",
|
| 52 |
undo_btn="Undo last message",
|
| 53 |
clear_btn="Start a new conversation"
|
| 54 |
).queue()
|
| 55 |
+
|
| 56 |
chat_history_json = gr.JSON(generate_json(chat_history))
|
| 57 |
gr.Markdown("### 📩 Generate the JSON file for your chat history!")
|
| 58 |
+
gr.Interface(fn=generate_json,
|
| 59 |
+
inputs=None,
|
| 60 |
+
outputs=[chat_history_json])
|
| 61 |
+
|
| 62 |
demo.queue()
|
| 63 |
+
demo.launch()
|