Alexander Hux commited on
Commit
911e5a0
·
verified ·
1 Parent(s): ee3d88e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+
5
+ # Set your OpenAI API key from environment variable
6
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
7
+
8
+ # System prompt or initial role message
9
+ SYSTEM_PROMPT = {
10
+ "role": "system",
11
+ "content": "You are ZEN AI, a helpful assistant from zenai.world, trained to assist users with valuable insights and solutions related to AI literacy, entrepreneurial strategies, and opportunities for growth."
12
+ }
13
+
14
+ def process_message(history, user_input, model, temperature):
15
+ # history: list of tuples (user, assistant)
16
+ # user_input: current user message
17
+ # model: model name (e.g., "gpt-4")
18
+ # temperature: float value for creativity
19
+
20
+ # Convert history to the format required by OpenAI
21
+ messages = [SYSTEM_PROMPT]
22
+ for user_msg, bot_msg in history:
23
+ messages.append({"role": "user", "content": user_msg})
24
+ messages.append({"role": "assistant", "content": bot_msg})
25
+
26
+ # Add the new user message
27
+ messages.append({"role": "user", "content": user_input})
28
+
29
+ # Get the response from OpenAI
30
+ response = openai.ChatCompletion.create(
31
+ model=model,
32
+ messages=messages,
33
+ temperature=temperature,
34
+ max_tokens=1024
35
+ )
36
+
37
+ # Extract and return the assistant's reply
38
+ assistant_reply = response.choices[0].message["content"]
39
+ return assistant_reply
40
+
41
+ def chat_interface(user_input, history, model, temperature):
42
+ bot_reply = process_message(history, user_input, model, temperature)
43
+ history.append((user_input, bot_reply))
44
+ return history, history
45
+
46
+ # Define Gradio blocks
47
+ with gr.Blocks(theme="default") as demo:
48
+ gr.Markdown("<h1 align='center'>ZEN AI Chatbot</h1>")
49
+ gr.Markdown("Welcome to ZEN AI! Ask me anything about AI literacy, scaling businesses, and connecting with global opportunities.")
50
+
51
+ with gr.Row():
52
+ model = gr.Dropdown(
53
+ ["gpt-3.5-turbo", "gpt-4"],
54
+ value="gpt-4",
55
+ label="OpenAI Model"
56
+ )
57
+ temperature = gr.Slider(
58
+ minimum=0,
59
+ maximum=1,
60
+ value=0.7,
61
+ step=0.1,
62
+ label="Temperature"
63
+ )
64
+
65
+ chatbot = gr.Chatbot()
66
+ state = gr.State([]) # maintain the conversation history
67
+
68
+ with gr.Row():
69
+ user_input = gr.Textbox(
70
+ lines=1,
71
+ placeholder="Ask your question here...",
72
+ label="Your Message"
73
+ )
74
+ submit_button = gr.Button("Submit")
75
+
76
+ submit_button.click(
77
+ chat_interface,
78
+ inputs=[user_input, state, model, temperature],
79
+ outputs=[chatbot, state]
80
+ )
81
+
82
+ # Optionally, allow the user to clear the conversation
83
+ clear_button = gr.Button("Clear Conversation")
84
+ clear_button.click(lambda: [], None, chatbot)
85
+ clear_button.click(lambda: [], None, state)
86
+
87
+ demo.launch(server_name="0.0.0.0", share=True)