Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,47 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
-
from groq import
|
4 |
-
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
st.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
if
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
)
|
49 |
-
ai_response = chat_completion.choices[0].message.content
|
50 |
-
|
51 |
-
# Add AI response to the chat history
|
52 |
-
st.session_state.messages.append({"role": "assistant", "content": ai_response})
|
53 |
-
|
54 |
-
# Display the AI's response
|
55 |
-
st.write(f"**AI:** {ai_response}")
|
56 |
-
except Exception as e:
|
57 |
-
st.error(f"Error: {e}")
|
58 |
-
else:
|
59 |
-
st.warning("Please enter a message before sending.")
|
60 |
-
|
61 |
-
# Clear conversation button
|
62 |
-
if st.button("Clear Conversation"):
|
63 |
-
st.session_state.messages = []
|
64 |
-
|
65 |
-
# Footer
|
66 |
-
st.markdown("""
|
67 |
-
---
|
68 |
-
Powered by [Groq](https://groq.com) | Deployed on [Hugging Face Spaces](https://huggingface.co/spaces)
|
69 |
-
""")
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
+
from groq import Client
|
4 |
+
|
5 |
+
# Function to initialize Groq client
|
6 |
+
def get_groq_client():
|
7 |
+
api_key = os.getenv("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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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|