File size: 1,649 Bytes
f5a8bf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b97710b
f5a8bf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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(model_name="models/gemini-pro")
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):
    #response  = model.generate_content(question)
    question = question +str(' Answer shortly')
    response = chat.send_message(question,stream=True)
    #to_markdown(response.text)
    #print('Response:',response.text)
    #response = to_markdown(response.text)
    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))
        #st.write("Bot:", bot_response)
    else:
        st.warning("Please enter something.")
# Function to interact with OpenAI API and get response
for role,text in st.session_state['chat_history']:
    st.write(f"{role}:{text}")