mishiawan commited on
Commit
3f247af
·
verified ·
1 Parent(s): 0d3da25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -31
app.py CHANGED
@@ -3,39 +3,71 @@ from dotenv import load_dotenv
3
  from groq import Groq
4
  import streamlit as st
5
 
6
- # Load environment variables
7
  load_dotenv()
8
 
9
- GROQ_API_KEY = "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"
10
- client = Groq(api_key =GROQ_API_KEY)
11
 
12
- # Streamlit UI
13
- st.title("Python Bot with Groq's API")
14
- st.subheader("Interact with an AI Model Powered by Groq")
 
 
15
 
16
- # Input Section
17
- st.header("Input")
18
- user_input = st.text_input("Enter your question or message below:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Submit button
21
- if st.button("Submit"):
22
- if user_input.strip():
23
- # Output Section
24
- st.header("Output")
25
- with st.spinner("Fetching response from the LLM..."):
26
- try:
27
- # Interact with Groq's LLM
28
- chat_completion = client.chat.completions.create(
29
- messages=[
30
- {"role": "user", "content": user_input},
31
- ],
32
- model="llama3-8b-8192",
33
- stream=False,
34
- )
35
- response = chat_completion.choices[0].message.content
36
- st.success("Here is the AI's response:")
37
- st.write(response)
38
- except Exception as e:
39
- st.error(f"Error: {e}")
40
- else:
41
- st.warning("Please enter a valid message!")
 
 
 
 
 
 
 
 
 
3
  from groq import Groq
4
  import streamlit as st
5
 
6
+ # Load environment variables from .env file
7
  load_dotenv()
8
 
9
+ # Ensure that the API key is loaded correctly
10
+ api_key = os.getenv("GROQ_API_KEY")
11
 
12
+ if not api_key:
13
+ st.error("API key is missing! Please check your .env file or set the environment variable.")
14
+ else:
15
+ # Initialize Groq client with the API key
16
+ client = Groq(api_key=api_key)
17
 
18
+ # Custom CSS to style headings and overall appearance
19
+ st.markdown("""
20
+ <style>
21
+ .big-heading {
22
+ font-size: 36px;
23
+ font-weight: bold;
24
+ color: #333;
25
+ }
26
+ .sub-header {
27
+ font-size: 24px;
28
+ font-weight: bold;
29
+ color: #555;
30
+ }
31
+ .header {
32
+ font-size: 20px;
33
+ font-weight: normal;
34
+ color: #444;
35
+ }
36
+ .output-text {
37
+ font-size: 18px;
38
+ font-weight: normal;
39
+ color: #000;
40
+ }
41
+ </style>
42
+ """, unsafe_allow_html=True)
43
 
44
+ # Streamlit UI
45
+ st.markdown('<p class="big-heading">Python Bot with Groq\'s API</p>', unsafe_allow_html=True)
46
+ st.markdown('<p class="sub-header">Interact with an AI Model Powered by Groq</p>', unsafe_allow_html=True)
47
+
48
+ # Input Section
49
+ st.markdown('<p class="header">Input</p>', unsafe_allow_html=True)
50
+ user_input = st.text_input("Enter your question or message below:")
51
+
52
+ # Submit button
53
+ if st.button("Submit"):
54
+ if user_input.strip():
55
+ # Output Section
56
+ st.markdown('<p class="header">Output</p>', unsafe_allow_html=True)
57
+ with st.spinner("Fetching response from the LLM..."):
58
+ try:
59
+ # Interact with Groq's LLM
60
+ chat_completion = client.chat.completions.create(
61
+ messages=[
62
+ {"role": "user", "content": user_input},
63
+ ],
64
+ model="llama3-8b-8192",
65
+ stream=False,
66
+ )
67
+ response = chat_completion.choices[0].message.content
68
+ st.success("Here is the AI's response:")
69
+ st.markdown(f'<p class="output-text">{response}</p>', unsafe_allow_html=True)
70
+ except Exception as e:
71
+ st.error(f"Error: {e}")
72
+ else:
73
+ st.warning("Please enter a valid message!")