Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
from groq import Groq | |
# Custom function to load variables from a .env file | |
def load_dotenv(filepath=".env"): | |
try: | |
with open(filepath, "r") as f: | |
for line in f: | |
line = line.strip() | |
# Skip comments and empty lines | |
if not line or line.startswith("#"): | |
continue | |
if "=" in line: | |
key, value = line.split("=", 1) | |
os.environ.setdefault(key.strip(), value.strip()) | |
except FileNotFoundError: | |
st.error("No .env file found. Please create one with your API key.") | |
except Exception as e: | |
st.error(f"Error loading .env file: {e}") | |
# Load environment variables from the .env file | |
load_dotenv() | |
def main(): | |
st.title("Samyotech Chatbot") | |
# Fetch API key from environment variables | |
groq_api_key = os.environ.get("GROQ_API_KEY") | |
if groq_api_key: | |
client = Groq(api_key=groq_api_key) | |
st.write("### Chat with LLM") | |
user_input = st.text_area("Enter your message:") | |
if st.button("Generate Response"): | |
if user_input.strip(): | |
try: | |
response = client.chat.completions.create( | |
model="llama-3.3-70b-versatile", | |
messages=[ | |
{"role": "user", "content": user_input} | |
] | |
) | |
st.write("### Response:") | |
st.write(response.choices[0].message.content) | |
except Exception as e: | |
st.error(f"Error: {e}") | |
else: | |
st.warning("Please enter a message to generate a response.") | |
else: | |
st.error("API key not found. Please ensure your .env file contains GROQ_API_KEY.") | |
if __name__ == "__main__": | |
main() | |