deter3 commited on
Commit
940d88e
1 Parent(s): c30b9ce

Create chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +91 -0
chatbot.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import os
4
+ from functools import partial
5
+
6
+ import chat_agent
7
+ from langchain.schema import (
8
+ HumanMessage
9
+ )
10
+
11
+
12
+ def set_api_key(api_key):
13
+ openai.api_key = api_key
14
+ os.environ["OPENAI_API_KEY"] = api_key
15
+ return "API Key set successfully."
16
+
17
+
18
+ def get_response(chatbot, api_key, selected_model, user_input, conversation_history=""):
19
+ set_api_key(api_key)
20
+
21
+ # Preserve the memory of the current chatbot
22
+ preserved_memory = chatbot.memory
23
+
24
+ # Create a new chat agent based on the selected model and seed the memory
25
+ chatbot = chat_agent.create_chatbot(model_name=selected_model, seed_memory=preserved_memory)
26
+
27
+ # Get raw chat response
28
+ response = chatbot.agent.run(user_input).strip()
29
+
30
+ # Iterate through messages in ChatMessageHistory and format the output
31
+ updated_conversation = '<div style="background-color: hsl(30, 100%, 30%); color: white; padding: 5px; margin-bottom: 10px; text-align: center; font-size: 1.5em;">Chat History</div>'
32
+ for i, message in enumerate(chatbot.memory.chat_memory.messages):
33
+ if isinstance(message, HumanMessage):
34
+ prefix = "User: "
35
+ background_color = "hsl(0, 0%, 40%)" # Dark grey background
36
+ text_color = "hsl(0, 0%, 100%)" # White text
37
+ else:
38
+ prefix = "Chatbot: "
39
+ background_color = "hsl(0, 0%, 95%)" # White background
40
+ text_color = "hsl(0, 0%, 0%)" # Black text
41
+ updated_conversation += f'<div style="color: {text_color}; background-color: {background_color}; margin: 5px; padding: 5px;">{prefix}{message.content}</div>'
42
+ return updated_conversation
43
+
44
+
45
+ def main():
46
+ api_key = os.environ.get("OPENAI_API_KEY") # "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"
47
+
48
+ api_key_input = gr.components.Textbox(
49
+ lines=1,
50
+ label="Enter OpenAI API Key",
51
+ value=api_key,
52
+ type="password",
53
+ )
54
+
55
+ model_selection = gr.components.Dropdown(
56
+ choices=["gpt-3.5-turbo"],
57
+ label="Select a GPT Model",
58
+ value="gpt-3.5-turbo",
59
+ )
60
+
61
+ user_input = gr.components.Textbox(
62
+ lines=3,
63
+ label="Enter your message",
64
+ )
65
+
66
+ output_history = gr.outputs.HTML(
67
+ label="Updated Conversation",
68
+ )
69
+
70
+ chatbot = chat_agent.create_chatbot(model_name=model_selection.value) # model_selection.value
71
+
72
+ inputs = [
73
+ api_key_input,
74
+ model_selection,
75
+ user_input,
76
+ ]
77
+
78
+ iface = gr.Interface(
79
+ fn=partial(get_response, chatbot),
80
+ inputs=inputs,
81
+ outputs=[output_history],
82
+ title="Everything About Martin SeligMan & Positive Psychology",
83
+ description=" OTHER PEOPLE MATTER ",
84
+ allow_flagging="never",
85
+ )
86
+
87
+ iface.launch()
88
+
89
+
90
+ if __name__ == "__main__":
91
+ main()