arhanovich commited on
Commit
0650e1f
1 Parent(s): a2cccd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -37
app.py CHANGED
@@ -1,40 +1,40 @@
1
- """
2
- If you are running this code, replace API_KEY in config.json with actual api key obtained from https://console.groq.com/keys
3
- Also, please grab the public URL and then save it into config.json and keep the program runnig.
4
- """
5
-
6
  import gradio as gr
 
7
  from groq import Groq
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- api_key = config['API_KEY']
10
- if api_key == "your-api-key-here":
11
- print("Please replac API_KEY in config.json with actual api key obtained from https://console.groq.com/keys")
12
- else:
13
- client = Groq(api_key=api_key)
14
-
15
- messages = [
16
- {"role": "system", "content": "Act as though you are Bart Simpson"}
17
- ]
18
- print("!!!!After the launch is done, please grab the public URL and then save it into config.json and keeo the program running")
19
-
20
- def respond(message, chat_history):
21
- messages.append({"role": "user", "content": message})
22
- chat_completion = client.chat.completions.create(
23
- messages=messages,
24
- model="llama3-8b-8192",
25
- )
26
- bot_message = chat_completion.choices[0].message.content
27
- messages.append({"role": "assistant", "content": bot_message})
28
- chat_history.append((message, bot_message))
29
- return "", chat_history
30
-
31
- with gr.Blocks() as demo:
32
- chatbot = gr.Chatbot()
33
- msg = gr.Textbox(placeholder="Type a message and press Enter")
34
- clear = gr.ClearButton([msg, chatbot])
35
-
36
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
37
-
38
- if __name__ == "__main__":
39
- demo.launch()
40
-
 
1
+ import os
 
 
 
 
2
  import gradio as gr
3
+ import json
4
  from groq import Groq
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+
10
+ # Get the API key from the environment variable
11
+ api_key = os.getenv('API_KEY')
12
+ if not api_key or api_key == "your-api-key-here":
13
+ raise ValueError("Please set the API_KEY in the .env file or as an environment variable")
14
+
15
+ client = Groq(api_key=api_key)
16
+
17
+ messages = [
18
+ {"role": "system", "content": "Act as though you are Bart Simpson"}
19
+ ]
20
+
21
+ def respond(message, chat_history):
22
+ messages.append({"role": "user", "content": message})
23
+ chat_completion = client.chat.completions.create(
24
+ messages=messages,
25
+ model="llama3-8b-8192",
26
+ )
27
+ bot_message = chat_completion.choices[0].message.content
28
+ messages.append({"role": "assistant", "content": bot_message})
29
+ chat_history.append((message, bot_message))
30
+ return "", chat_history
31
+
32
+ with gr.Blocks() as demo:
33
+ chatbot = gr.Chatbot()
34
+ msg = gr.Textbox(placeholder="Type a message and press Enter")
35
+ clear = gr.ClearButton([msg, chatbot])
36
+
37
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
38
 
39
+ if __name__ == "__main__":
40
+ demo.launch()