Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Set your OpenAI API key
|
6 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
7 |
+
|
8 |
+
def print_like_dislike(x: gr.LikeData):
|
9 |
+
print(x.index, x.value, x.liked)
|
10 |
+
|
11 |
+
def add_text(history, text):
|
12 |
+
history.append((text, "**That's cool!**"))
|
13 |
+
return history
|
14 |
+
|
15 |
+
def add_file(history, file):
|
16 |
+
# Assuming you want to display the name of the uploaded file
|
17 |
+
file_info = (f"Uploaded file: {file.name}", "")
|
18 |
+
history.append(file_info)
|
19 |
+
return history
|
20 |
+
|
21 |
+
def initialize_chat():
|
22 |
+
# This function initializes the chat with a "Hello" message.
|
23 |
+
return [(None, "Hello, my name is <strong>Andrea</strong>, I'm a <em>Friendly Chatbot</em> and will help you with your learning journey. <br>Select a question from below to start!")]
|
24 |
+
|
25 |
+
chat_history = initialize_chat()
|
26 |
+
|
27 |
+
def generate_response(selected_question):
|
28 |
+
global chat_history
|
29 |
+
prompt = selected_question # Ensure selected_question is a string
|
30 |
+
|
31 |
+
try:
|
32 |
+
response = openai.Completion.create(
|
33 |
+
engine="text-davinci-003",
|
34 |
+
prompt=prompt,
|
35 |
+
max_tokens=150,
|
36 |
+
n=1,
|
37 |
+
stop=None,
|
38 |
+
temperature=0.7,
|
39 |
+
).choices[0].text.strip()
|
40 |
+
|
41 |
+
topics_str = "Topic analysis not available"
|
42 |
+
|
43 |
+
# Adjusted to return a list of tuples as expected by the Chatbot component
|
44 |
+
new_response = (None, response + "\n\nTopics: " + topics_str)
|
45 |
+
chat_history.append(new_response)
|
46 |
+
except Exception as e:
|
47 |
+
new_response = (None, f"Error generating response: {e}")
|
48 |
+
chat_history.append(new_response)
|
49 |
+
|
50 |
+
return chat_history
|
51 |
+
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown(
|
54 |
+
"""
|
55 |
+
# Child safe chatbot project!
|
56 |
+
In the realm of digital communication, the development of an advanced chatbot that incorporates topic modeling represents a significant leap towards enhancing user interaction and maintaining focus during conversations. This innovative chatbot design is specifically engineered to streamline discussions by guiding users to select from a curated list of suggested questions. This approach is crafted to mitigate the risk of diverging into off-topic dialogues, which are common pitfalls in conventional chatbot systems.
|
57 |
+
"""
|
58 |
+
)
|
59 |
+
|
60 |
+
chatbot = gr.Chatbot(
|
61 |
+
initialize_chat(),
|
62 |
+
elem_id="chatbot",
|
63 |
+
bubble_full_width=False,
|
64 |
+
label="Safe Chatbot v1",
|
65 |
+
avatar_images=(None, os.path.join(os.getcwd(), "avatar.png"))
|
66 |
+
)
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
txt = gr.Textbox(scale=4, show_label=False, placeholder="Select Question", container=False, interactive=False) # Adjust based on need
|
70 |
+
btn = gr.Button("Submit")
|
71 |
+
|
72 |
+
btn.click(fn=generate_response, inputs=[txt], outputs=chatbot)
|
73 |
+
|
74 |
+
examples = [
|
75 |
+
["What are the basic requirements to become an airforce pilot?"],
|
76 |
+
["How long does it take to train as an airforce pilot?"],
|
77 |
+
["Can you describe a day in the life of an airforce pilot?"]
|
78 |
+
]
|
79 |
+
gr.Examples(examples, inputs=[txt], outputs=[chatbot], label="Select Question")
|
80 |
+
|
81 |
+
chatbot.like(print_like_dislike, None, None)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
demo.launch()
|