Spaces:
Sleeping
Sleeping
Commit
·
a81180f
1
Parent(s):
b409e48
app.py
CHANGED
|
@@ -1,27 +1,34 @@
|
|
| 1 |
-
import openai
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 7 |
for user_msg, bot_msg in history:
|
| 8 |
messages.append({"role": "user", "content": user_msg})
|
| 9 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 10 |
-
messages.append({"role": "user", "content": prompt})
|
| 11 |
|
|
|
|
|
|
|
| 12 |
response = openai.ChatCompletion.create(
|
| 13 |
-
model="gpt-4",
|
| 14 |
messages=messages
|
| 15 |
)
|
| 16 |
-
return response["choices"][0]["message"]["content"]
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
chat_history.append((user_input, response))
|
| 21 |
-
return response, chat_history
|
| 22 |
|
| 23 |
# Create a Gradio interface
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
| 26 |
if __name__ == "__main__":
|
| 27 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set your OpenAI API key (or use environment variables)
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
def chatbot(prompt, history=[]):
|
| 9 |
+
"""Generates a chatbot response using OpenAI's API."""
|
| 10 |
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 11 |
for user_msg, bot_msg in history:
|
| 12 |
messages.append({"role": "user", "content": user_msg})
|
| 13 |
messages.append({"role": "assistant", "content": bot_msg})
|
|
|
|
| 14 |
|
| 15 |
+
messages.append({"role": "user", "content": prompt})
|
| 16 |
+
|
| 17 |
response = openai.ChatCompletion.create(
|
| 18 |
+
model="gpt-4",
|
| 19 |
messages=messages
|
| 20 |
)
|
|
|
|
| 21 |
|
| 22 |
+
reply = response["choices"][0]["message"]["content"]
|
| 23 |
+
return reply
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Create a Gradio interface
|
| 26 |
+
chat_interface = gr.ChatInterface(
|
| 27 |
+
chatbot,
|
| 28 |
+
title="Simple OpenAI Chatbot",
|
| 29 |
+
description="A chatbot powered by OpenAI's GPT API. Start a conversation!",
|
| 30 |
+
)
|
| 31 |
|
| 32 |
+
# Launch on Hugging Face Spaces
|
| 33 |
if __name__ == "__main__":
|
| 34 |
+
chat_interface.launch()
|