| import pathlib |
| import textwrap |
| import streamlit as st |
| import google.generativeai as genai |
| from dotenv import load_dotenv |
| from IPython.display import Markdown |
| from IPython.display import display |
| import os |
|
|
| load_dotenv() |
| genai.configure(api_key="AIzaSyA_XykKxC4aSi0af9VH5uP2eQlp9Nh25Ds") |
| |
| model = genai.GenerativeModel("gemini-pro") |
| chat = model.start_chat(history=[]) |
|
|
| def to_markdown(text): |
| text = text.replace('•', ' *') |
| return Markdown(textwrap.indent(text,'> ',predicate=lambda _:True)) |
|
|
| def get_gemini_response(question): |
| |
| question = question +str(' Answer shortly') |
| response = chat.send_message(question,stream=True) |
| |
| |
| |
| return response |
|
|
| st.title("Google Powered Chatbot") |
|
|
| if 'chat_history' not in st.session_state: |
| st.session_state['chat_history'] = [] |
|
|
| user_input = st.text_input("Input:",key='input') |
| submit_button = st.button("Ask Question") |
|
|
| if submit_button and user_input: |
| if user_input: |
| bot_response = get_gemini_response(user_input) |
| st.session_state['chat_history'].append(("You",user_input)) |
| for chunk in bot_response: |
| st.write(chunk.text) |
| st.session_state['chat_history'].append(("Bot",chunk.text)) |
| |
| else: |
| st.warning("Please enter something.") |
| |
| for role,text in st.session_state['chat_history']: |
| st.write(f"{role}:{text}") |
|
|
|
|