CHATBOT / app.py
Memoona648's picture
Create app.py
27109da verified
import streamlit as st
from groq import Groq
import os
# πŸ” Get API key from Hugging Face secrets
api_key = os.getenv("GROQ_API_KEY")
# Create client
client = Groq(api_key=api_key)
# πŸ”€ Manual Tokenizer
def manual_tokenizer(text):
text = text.lower()
text = text.replace(".", "").replace(",", "").replace("!", "").replace("?", "")
tokens = text.split()
return tokens
# πŸ€– Chat Function
def chat_with_groq(user_input, messages):
tokens = manual_tokenizer(user_input)
processed_text = " ".join(tokens)
messages.append({"role": "user", "content": processed_text})
response = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=messages
)
reply = response.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
# 🎨 Streamlit UI
st.set_page_config(page_title="Chatbot", page_icon="πŸ’¬")
st.title("πŸ’¬ AI Chatbot (Groq + Streamlit)")
# Session state for memory
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "system", "content": "You are a helpful chatbot."}
]
# Chat input
user_input = st.text_input("You:")
if st.button("Send") and user_input:
reply = chat_with_groq(user_input, st.session_state.messages)
# Display chat
for msg in st.session_state.messages[1:]:
if msg["role"] == "user":
st.write(f"πŸ‘€ You: {msg['content']}")
else:
st.write(f"πŸ€– Bot: {msg['content']}")