|
|
import os |
|
|
import streamlit as st |
|
|
from datetime import datetime |
|
|
from dotenv import load_dotenv |
|
|
import google.generativeai as genai |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
api_key = "AIzaSyCgwV3oXhiERE9l_tB1-RM4j4HFDSHd4jI" |
|
|
|
|
|
|
|
|
genai.configure(api_key=api_key) |
|
|
|
|
|
|
|
|
model = genai.GenerativeModel('gemini-1.5-flash') |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Bhai Bot", page_icon="π€", layout="centered") |
|
|
|
|
|
|
|
|
st.markdown(""" |
|
|
<style> |
|
|
.stTextInput > label { |
|
|
font-size: 20px; |
|
|
color: #333; |
|
|
} |
|
|
.stButton > button { |
|
|
font-size: 18px; |
|
|
padding: 10px; |
|
|
background-color: #4CAF50; |
|
|
color: white; |
|
|
} |
|
|
.message { |
|
|
padding: 10px; |
|
|
margin: 5px 0; |
|
|
border-radius: 10px; |
|
|
max-width: 70%; |
|
|
} |
|
|
.user_message { |
|
|
background-color: #DCF8C6; |
|
|
align-self: flex-end; |
|
|
} |
|
|
.bot_message { |
|
|
background-color: #E2E2E2; |
|
|
align-self: flex-start; |
|
|
} |
|
|
.timestamp { |
|
|
font-size: 12px; |
|
|
color: #888; |
|
|
} |
|
|
</style> |
|
|
""", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
|
st.session_state["messages"] = [] |
|
|
|
|
|
|
|
|
def chatbot_response(user_input): |
|
|
|
|
|
if "bhai" in user_input.lower(): |
|
|
|
|
|
bot_name = "bhai" |
|
|
response_prefix = f"Haan {bot_name}, " |
|
|
else: |
|
|
response_prefix = "" |
|
|
|
|
|
|
|
|
prompt = f"{response_prefix}apni baat sirf Roman Urdu mein bata: {user_input}" |
|
|
|
|
|
|
|
|
response = model.generate_content(prompt) |
|
|
|
|
|
|
|
|
return response.text |
|
|
|
|
|
|
|
|
def send_message(): |
|
|
user_input = st.session_state.input |
|
|
if user_input: |
|
|
|
|
|
st.session_state["messages"].append({ |
|
|
"role": "user", |
|
|
"content": user_input, |
|
|
"time": datetime.now().strftime("%H:%M:%S") |
|
|
}) |
|
|
|
|
|
|
|
|
bot_reply = chatbot_response(user_input) |
|
|
|
|
|
|
|
|
st.session_state["messages"].append({ |
|
|
"role": "bot", |
|
|
"content": bot_reply, |
|
|
"time": datetime.now().strftime("%H:%M:%S") |
|
|
}) |
|
|
|
|
|
|
|
|
st.session_state.input = "" |
|
|
|
|
|
|
|
|
st.title("π€ Bhai Bot (Roman Urdu)") |
|
|
|
|
|
|
|
|
for message in st.session_state["messages"]: |
|
|
if message["role"] == "user": |
|
|
st.markdown(f'<div class="message user_message">{message["content"]}<div class="timestamp">{message["time"]}</div></div>', unsafe_allow_html=True) |
|
|
else: |
|
|
st.markdown(f'<div class="message bot_message">{message["content"]}<div class="timestamp">{message["time"]}</div></div>', unsafe_allow_html=True) |
|
|
|
|
|
|
|
|
st.text_input("Likho apni baat...", key="input", on_change=send_message) |
|
|
|