Alexander Hux
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,40 @@
|
|
1 |
-
import os
|
2 |
-
import openai
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
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 |
-
|
66 |
-
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
)
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|