Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,40 @@
|
|
| 1 |
-
import os
|
| 2 |
import openai
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
openai.api_key = os.getenv(
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
prompt
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
block = gr.Blocks()
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
with block:
|
| 46 |
-
gr.Markdown("""<h1><center>Build Yo'own ChatGPT with OpenAI API & Gradio</center></h1>
|
| 47 |
-
""")
|
| 48 |
-
chatbot = gr.Chatbot()
|
| 49 |
-
message = gr.Textbox(placeholder=prompt)
|
| 50 |
-
state = gr.State()
|
| 51 |
-
submit = gr.Button("SEND")
|
| 52 |
-
submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])
|
| 53 |
-
|
| 54 |
-
block.launch(debug = True)
|
|
|
|
|
|
|
| 1 |
import openai
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Set OpenAI API key from environment variable
|
| 6 |
+
openai.api_key = os.getenv(OPENAI_API_KEY)
|
| 7 |
+
|
| 8 |
+
# Define a function to interact with OpenAI's ChatGPT
|
| 9 |
+
def chat_with_openai(input_text):
|
| 10 |
+
try:
|
| 11 |
+
response = openai.Completion.create(
|
| 12 |
+
engine="gpt-4", # Use the model you want (e.g., 'gpt-3.5-turbo', 'gpt-4')
|
| 13 |
+
prompt=input_text,
|
| 14 |
+
max_tokens=150,
|
| 15 |
+
n=1,
|
| 16 |
+
stop=None,
|
| 17 |
+
temperature=0.7,
|
| 18 |
+
)
|
| 19 |
+
answer = response.choices[0].text.strip()
|
| 20 |
+
return answer
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return str(e)
|
| 23 |
+
|
| 24 |
+
# Define the Gradio interface
|
| 25 |
+
with gr.Blocks() as ui:
|
| 26 |
+
gr.Markdown("# Chatbot with OpenAI API")
|
| 27 |
+
|
| 28 |
+
chatbot = gr.Chatbot(label="OpenAI Chatbot")
|
| 29 |
+
msg = gr.Textbox(label="Enter your message here:")
|
| 30 |
+
submit_btn = gr.Button("Submit")
|
| 31 |
+
|
| 32 |
+
def on_submit(message, chat_history):
|
| 33 |
+
response = chat_with_openai(message)
|
| 34 |
+
chat_history.append((message, response))
|
| 35 |
+
return chat_history, ""
|
| 36 |
+
|
| 37 |
+
submit_btn.click(on_submit, inputs=[msg, chatbot], outputs=[chatbot, msg])
|
| 38 |
+
|
| 39 |
+
# Launch the Gradio app
|
| 40 |
+
ui.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|