Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def chat_with_openai(api_key, user_input):
|
5 |
+
"""
|
6 |
+
Function to send user input to OpenAI’s Chat Completion API and return the response.
|
7 |
+
"""
|
8 |
+
openai.api_key = api_key
|
9 |
+
response = openai.ChatCompletion.create(
|
10 |
+
model='gpt-3.5-turbo', # You can change the model as needed
|
11 |
+
messages=[
|
12 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
13 |
+
{"role": "user", "content": user_input},
|
14 |
+
]
|
15 |
+
)
|
16 |
+
|
17 |
+
return response.choices[0].message['content']
|
18 |
+
|
19 |
+
def main():
|
20 |
+
description = "This is a simple interface to interact with OpenAI’s Chat Completion API. Please enter your API key and your message."
|
21 |
+
with gr.Blocks() as demo:
|
22 |
+
with gr.Row():
|
23 |
+
api_key_input = gr.Textbox(label="API Key", placeholder="Enter your OpenAI API key here", show_label=True, type="password")
|
24 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Enter your message here")
|
25 |
+
submit_btn = gr.Button("Submit")
|
26 |
+
output = gr.Textbox(label="Chatbot Response")
|
27 |
+
|
28 |
+
submit_btn.click(fn=chat_with_openai, inputs=[api_key_input, user_input], outputs=output)
|
29 |
+
|
30 |
+
demo.launch()
|
31 |
+
|
32 |
+
if __name__ == "__main__":
|
33 |
+
main()
|