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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -83
app.py CHANGED
@@ -1,87 +1,40 @@
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)
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import os
4
 
5
+ # Retrieve the API key from the environment variables
6
+ API_KEY = os.getenv("OPENAI_API_KEY")
7
+ API_URL = "https://api.openai.com/v1/chat/completions"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Function to call the OpenAI API
10
+ def chat_with_model(prompt):
11
+ if not API_KEY:
12
+ return "Error: API key not found. Please set it in the environment variables."
13
 
14
+ headers = {
15
+ "Content-Type": "application/json",
16
+ "Authorization": f"Bearer {API_KEY}"
17
+ }
18
+ payload = {
19
+ "model": "gpt-4", # Replace with your desired model (e.g., gpt-3.5-turbo, gpt-4)
20
+ "messages": [{"role": "user", "content": prompt}]
21
+ }
22
+
23
+ try:
24
+ response = requests.post(API_URL, json=payload, headers=headers)
25
+ response.raise_for_status()
26
+ return response.json()["choices"][0]["message"]["content"]
27
+ except requests.exceptions.RequestException as e:
28
+ return f"Error: {str(e)}"
29
+
30
+ # Gradio Interface
31
+ iface = gr.Interface(
32
+ fn=chat_with_model,
33
+ inputs="text",
34
+ outputs="text",
35
+ title="ZEN AI Chatbot",
36
+ description="A simple chatbot powered by OpenAI's GPT models."
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ iface.launch()