Spaces:
Sleeping
Sleeping
Commit
·
f81a945
1
Parent(s):
d1720a8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
# Set your OpenAI API key
|
5 |
+
openai.api_key = "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"
|
6 |
+
|
7 |
+
# Initialize chat history with a system message
|
8 |
+
messages = [{"role": "system", "content": "YOU ARE THE BEST DEVELOPER"}]
|
9 |
+
|
10 |
+
# Define the chatbot function
|
11 |
+
def CustomChatGPT(user_message):
|
12 |
+
try:
|
13 |
+
# Append the user's message to the chat history
|
14 |
+
messages.append({"role": "user", "content": user_message})
|
15 |
+
|
16 |
+
# Generate a response from the chatbot
|
17 |
+
response = openai.ChatCompletion.create(
|
18 |
+
model="gpt-3.5-turbo",
|
19 |
+
messages=messages
|
20 |
+
)
|
21 |
+
|
22 |
+
# Extract the assistant's reply from the response
|
23 |
+
assistant_reply = response["choices"][0]["message"]["content"]
|
24 |
+
|
25 |
+
# Append the assistant's reply to the chat history
|
26 |
+
messages.append({"role": "assistant", "content": assistant_reply})
|
27 |
+
|
28 |
+
return assistant_reply
|
29 |
+
except Exception as e:
|
30 |
+
return f"An error occurred: {str(e)}"
|
31 |
+
|
32 |
+
# Create a Gradio interface for the chatbot
|
33 |
+
interface = gr.Interface(
|
34 |
+
fn=CustomChatGPT,
|
35 |
+
inputs=gr.inputs.Textbox(placeholder="Ask me anything!", label="Chat with the Best Developer's Assistant"),
|
36 |
+
outputs=gr.outputs.Textbox(),
|
37 |
+
title="ChatGPT - The Best Developer's Assistant",
|
38 |
+
description="Ask questions or seek assistance from ChatGPT! This AI-powered assistant is here to help you.",
|
39 |
+
|
40 |
+
)
|
41 |
+
|
42 |
+
# Customize the interface with CSS styles
|
43 |
+
interface.css = """
|
44 |
+
.chat-message { border-radius: 10px; padding: 10px; margin: 5px; max-width: 80%; }
|
45 |
+
.user-message { background-color: #e1f0fa; float: right; }
|
46 |
+
.assistant-message { background-color: #f9f9f9; float: left; }
|
47 |
+
.output { background-color: #f9f9f9; padding: 10px; border-radius: 10px; }
|
48 |
+
.input { border-radius: 10px; }
|
49 |
+
.gradio-interface { max-width: 600px; margin: 0 auto; } /* Center the interface */
|
50 |
+
.gradio-interface .btn-primary { background-color: #0078d4; border: none; }
|
51 |
+
.gradio-interface .btn-primary:hover { background-color: #005a9e; }
|
52 |
+
.gradio-interface .btn-secondary { background-color: #f9f9f9; color: #0078d4; border: none; }
|
53 |
+
.gradio-interface .btn-secondary:hover { background-color: #e1f0fa; }
|
54 |
+
"""
|
55 |
+
|
56 |
+
# Launch the Gradio interface
|
57 |
+
interface.launch(share=True)
|