saherPervaiz commited on
Commit
556ffec
·
verified ·
1 Parent(s): 5ccb64a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -37
app.py CHANGED
@@ -1,47 +1,29 @@
1
  import os
2
  import streamlit as st
3
- from groq import Groq
4
 
5
- # Function to initialize Groq client
6
- def get_groq_client():
7
- api_key = "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"
8
- if not api_key:
9
- st.error("API key is missing. Please set the GROQ_API_KEY environment variable.")
10
- return None
11
- return Client(api_key=api_key)
12
 
13
- # Function to get chatbot response
14
- def get_response(client, user_input):
 
 
 
 
15
  try:
16
- response = client.chat.completions.create(
 
 
 
 
17
  messages=[{"role": "user", "content": user_input}],
18
  model="llama-3.3-70b-versatile"
19
  )
20
- return response['choices'][0]['message']['content']
 
 
 
 
21
  except Exception as e:
22
  st.error(f"Error: {e}")
23
- return None
24
-
25
- # Streamlit UI
26
- def main():
27
- st.title("AI Chatbot using Groq")
28
-
29
- # Get the Groq client
30
- client = get_groq_client()
31
- if not client:
32
- return
33
-
34
- # User input
35
- user_input = st.text_input("Ask a question:")
36
- if st.button("Get Answer"):
37
- if user_input:
38
- with st.spinner("Thinking..."):
39
- answer = get_response(client, user_input)
40
- if answer:
41
- st.success("Response:")
42
- st.write(answer)
43
- else:
44
- st.warning("Please enter a message.")
45
-
46
- if __name__ == "__main__":
47
- main()
 
1
  import os
2
  import streamlit as st
3
+ from groq import * # Import everything from groq, so you can access the chat functionality
4
 
5
+ # Ensure you have the correct API key set in the environment variable
6
+ api_key = "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"
 
 
 
 
 
7
 
8
+ # Streamlit interface
9
+ st.title("AI Chatbot with Groq")
10
+
11
+ user_input = st.text_input("Ask something:", "")
12
+
13
+ if user_input:
14
  try:
15
+ # Create the Groq client using the API key
16
+ client = Groq(api_key=api_key)
17
+
18
+ # Create chat completion with user input
19
+ chat_completion = client.chat.completions.create(
20
  messages=[{"role": "user", "content": user_input}],
21
  model="llama-3.3-70b-versatile"
22
  )
23
+
24
+ # Display the response
25
+ st.write("AI's response: ")
26
+ st.write(chat_completion.choices[0].message.content)
27
+
28
  except Exception as e:
29
  st.error(f"Error: {e}")