File size: 2,729 Bytes
6c360ab
 
 
dc0a3a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c360ab
1ac519d
6c360ab
 
1bbb715
dc0a3a2
1bbb715
 
 
dc0a3a2
1bbb715
 
 
 
dc0a3a2
1bbb715
dc0a3a2
1bbb715
 
dc0a3a2
6c360ab
1bbb715
dc0a3a2
1bbb715
 
 
 
 
 
 
 
 
 
dc0a3a2
1bbb715
 
dc0a3a2
1bbb715
dc0a3a2
6c360ab
 
 
dc0a3a2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
88
89
90
91
92
93
94
95
96
97
import streamlit as st
from groq import Groq

# Custom CSS for styling
def add_custom_css():
    st.markdown(
        """
        <style>
        body {
            background-color: #f7f9fc;
            color: #333;
            font-family: 'Arial', sans-serif;
        }
        .main > div {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        input[type="password"], input[type="text"] {
            border: 2px solid #ccc;
            border-radius: 5px;
            padding: 10px;
            margin-bottom: 10px;
            width: 100%;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .stMarkdown {
            font-size: 16px;
        }
        </style>
        """,
        unsafe_allow_html=True
    )

# Add custom CSS
add_custom_css()

# Streamlit App
st.title("Q&A Application with Groq API")
st.write("Ask a question and get a response using Groq's AI model!")

# Input for API key
api_key = st.text_input("πŸ”‘ Enter your GROQ API Key:", type="password")

# Check if API key is provided
if not api_key:
    st.warning("⚠️ Please enter your GROQ API Key to proceed.")
else:
    # Initialize the Groq client
    try:
        client = Groq(api_key=api_key)
        st.success("βœ… GROQ API Key successfully set!")
    except Exception as e:
        st.error(f"❌ Error initializing Groq client: {str(e)}")

    # Input prompt from the user
    user_input = st.text_input("πŸ’¬ Enter your question:", "")

    # Button to trigger the API call
    if st.button("πŸš€ Get Answer"):
        if user_input.strip():
            try:
                # API call to Groq
                chat_completion = client.chat.completions.create(
                    messages=[{"role": "user", "content": user_input}],
                    model="llama3-8b-8192"
                )
                # Extracting the response
                response = chat_completion.choices[0].message.content
                # Display the response
                st.subheader("πŸ€– Response:")
                st.write(response)
            except Exception as e:
                st.error(f"❌ Error: {str(e)}")
        else:
            st.warning("⚠️ Please enter a valid question.")

# Footer
st.markdown("---")
st.markdown(
    "<div style='text-align: center;'>✨ <strong>Powered by Groq AI</strong> ✨</div>",
    unsafe_allow_html=True
)