Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| from groq import Groq | |
| import httpx | |
| # Disable proxy environment variables completely | |
| for var in ['http_proxy', 'https_proxy', 'HTTP_PROXY', 'HTTPS_PROXY']: | |
| if var in os.environ: | |
| del os.environ[var] | |
| # Page configuration | |
| st.set_page_config( | |
| page_title="Groq AI Chat", | |
| page_icon="π€", | |
| layout="wide", | |
| initial_sidebar_state="expanded" | |
| ) | |
| # Custom CSS | |
| st.markdown(""" | |
| <style> | |
| .main { | |
| padding: 2rem; | |
| } | |
| .stChatMessage { | |
| padding: 1rem; | |
| border-radius: 0.5rem; | |
| margin-bottom: 1rem; | |
| } | |
| .css-1dp5vir { | |
| padding: 2rem; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Title and description | |
| st.title("π€ Groq AI Chat Assistant") | |
| st.markdown("β‘ Powered by Groq's lightning-fast LLM API - Get instant AI responses!") | |
| # Sidebar configuration | |
| with st.sidebar: | |
| st.header("βοΈ Configuration") | |
| st.markdown("---") | |
| # API Key input | |
| api_key = st.text_input( | |
| "Enter your Groq API Key", | |
| type="password", | |
| help="Get your free API key from https://console.groq.com" | |
| ) | |
| if not api_key: | |
| st.warning("π Please enter your Groq API key to proceed.") | |
| else: | |
| st.success("β API Key loaded") | |
| st.markdown("---") | |
| # Model selection | |
| model_option = st.selectbox( | |
| "Select Model", | |
| [ | |
| "llama-3.3-70b-versatile", | |
| "llama-3.1-8b-instant", | |
| "gemma2-9b-it" | |
| ], | |
| help="Choose the AI model to use", | |
| index=0 | |
| ) | |
| # Temperature slider | |
| temperature = st.slider( | |
| "Temperature (Creativity)", | |
| min_value=0.0, | |
| max_value=2.0, | |
| value=0.7, | |
| step=0.1, | |
| help="Higher = more creative, Lower = more focused" | |
| ) | |
| # Max tokens slider | |
| max_tokens = st.slider( | |
| "Max Tokens (Response Length)", | |
| min_value=100, | |
| max_value=2048, | |
| value=1024, | |
| step=100, | |
| help="Maximum length of response" | |
| ) | |
| st.markdown("---") | |
| # Clear chat button | |
| if st.button("ποΈ Clear Chat History", use_container_width=True): | |
| st.session_state.messages = [] | |
| st.rerun() | |
| st.markdown("---") | |
| # Info section | |
| st.markdown("**π Model Information**") | |
| model_info = { | |
| "llama-3.3-70b-versatile": "Most powerful model - Best for complex tasks", | |
| "llama-3.1-8b-instant": "Fastest responses - Good for quick questions", | |
| "gemma2-9b-it": "Balanced performance - Good all-rounder" | |
| } | |
| st.info(f"**{model_option}**\n\n{model_info[model_option]}") | |
| st.markdown("---") | |
| st.markdown("**π Resources**") | |
| st.markdown(""" | |
| - [Get Groq API Key](https://console.groq.com) | |
| - [Groq Documentation](https://console.groq.com/docs) | |
| - [API Status](https://status.groq.com) | |
| """) | |
| # Initialize session state for chat history | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display chat history | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"]): | |
| st.markdown(message["content"]) | |
| # Chat input and processing | |
| if api_key: | |
| if prompt := st.chat_input("Ask me anything..."): | |
| # Add user message to history | |
| st.session_state.messages.append({"role": "user", "content": prompt}) | |
| # Display user message | |
| with st.chat_message("user"): | |
| st.markdown(prompt) | |
| # Get AI response | |
| try: | |
| # Create HTTP client without proxy | |
| http_client = httpx.Client( | |
| timeout=30.0, | |
| limits=httpx.Limits(max_connections=5, max_keepalive_connections=2) | |
| ) | |
| # Initialize Groq client with custom HTTP client | |
| client = Groq( | |
| api_key=api_key, | |
| http_client=http_client | |
| ) | |
| with st.chat_message("assistant"): | |
| message_placeholder = st.empty() | |
| # Call Groq API | |
| response = client.chat.completions.create( | |
| model=model_option, | |
| messages=st.session_state.messages, | |
| temperature=temperature, | |
| max_tokens=max_tokens, | |
| top_p=1 | |
| ) | |
| assistant_message = response.choices[0].message.content | |
| message_placeholder.markdown(assistant_message) | |
| # Add assistant response to history | |
| st.session_state.messages.append({ | |
| "role": "assistant", | |
| "content": assistant_message | |
| }) | |
| # Close HTTP client | |
| http_client.close() | |
| except Exception as e: | |
| error_str = str(e).lower() | |
| full_error = str(e) | |
| if "proxies" in error_str or "proxy" in error_str: | |
| st.error("β Proxy Error - Trying alternative connection method...") | |
| st.info("Refresh the page and try again. If problem persists, restart the Space.") | |
| elif "authentication" in error_str or "401" in error_str or "invalid" in error_str or "api_error" in error_str: | |
| st.error("β Invalid API Key") | |
| st.warning("Please check your Groq API key:") | |
| st.info(""" | |
| 1. Go to https://console.groq.com | |
| 2. Click API Keys in sidebar | |
| 3. Copy your key (should start with 'gsk_') | |
| 4. Paste it above - NO extra spaces! | |
| 5. Make sure your account has credits | |
| """) | |
| with st.expander("Show full error"): | |
| st.code(full_error) | |
| elif "rate_limit" in error_str or "429" in error_str: | |
| st.error("β Rate limit reached - Please wait a moment and try again") | |
| elif "connection" in error_str: | |
| st.error("β Connection Error - Please check your internet connection") | |
| elif "decommissioned" in error_str or "model" in error_str: | |
| st.error("β Model Error - This model is no longer available") | |
| st.info("Try selecting a different model from the sidebar") | |
| else: | |
| st.error(f"β Error: {full_error}") | |
| st.info("Make sure you have an active Groq account with available credits") | |
| else: | |
| st.info("π Please enter your Groq API key in the sidebar to start chatting.") | |
| st.markdown("---") | |
| st.markdown("## π Getting Started") | |
| st.markdown(""" | |
| 1. **Get API Key**: Visit [console.groq.com](https://console.groq.com) and sign up (free) | |
| 2. **Create Key**: Go to API Keys section and create a new API key | |
| 3. **Paste Key**: Paste your key in the sidebar on the left | |
| 4. **Start Chatting**: Type your message and press Enter! | |
| ### Features | |
| - β‘ Lightning-fast responses | |
| - π― Multiple AI models to choose from | |
| - ποΈ Adjustable temperature and response length | |
| - πΎ Chat history management | |
| - π Works on desktop and mobile | |
| """) | |
| # Footer | |
| st.markdown("---") | |
| st.markdown(""" | |
| <div style="text-align: center; color: gray; font-size: 12px;"> | |
| Made with Streamlit & Groq API | Β© 2025 | |
| </div> | |
| """, unsafe_allow_html=True) |