Upload chatapp.py
Browse files- chatapp.py +50 -0
chatapp.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pathlib
|
| 2 |
+
import textwrap
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from IPython.display import Markdown
|
| 7 |
+
from IPython.display import display
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
genai.configure(api_key="AIzaSyA_XykKxC4aSi0af9VH5uP2eQlp9Nh25Ds")
|
| 12 |
+
#model = genai.GenerativeModel(model_name="models/gemini-pro")
|
| 13 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 14 |
+
chat = model.start_chat(history=[])
|
| 15 |
+
|
| 16 |
+
def to_markdown(text):
|
| 17 |
+
text = text.replace('•', ' *')
|
| 18 |
+
return Markdown(textwrap.indent(text,'> ',predicate=lambda _:True))
|
| 19 |
+
|
| 20 |
+
def get_gemini_response(question):
|
| 21 |
+
#response = model.generate_content(question)
|
| 22 |
+
|
| 23 |
+
response = chat.send_message(question,stream=True)
|
| 24 |
+
#to_markdown(response.text)
|
| 25 |
+
#print('Response:',response.text)
|
| 26 |
+
#response = to_markdown(response.text)
|
| 27 |
+
return response
|
| 28 |
+
|
| 29 |
+
st.title("Google Powered Chatbot")
|
| 30 |
+
|
| 31 |
+
if 'chat_history' not in st.session_state:
|
| 32 |
+
st.session_state['chat_history'] = []
|
| 33 |
+
|
| 34 |
+
user_input = st.text_input("Input:",key='input')
|
| 35 |
+
submit_button = st.button("Ask Question")
|
| 36 |
+
|
| 37 |
+
if submit_button and user_input:
|
| 38 |
+
if user_input:
|
| 39 |
+
bot_response = get_gemini_response(user_input)
|
| 40 |
+
st.session_state['chat_history'].append(("You",user_input))
|
| 41 |
+
for chunk in bot_response:
|
| 42 |
+
st.write(chunk.text)
|
| 43 |
+
st.session_state['chat_history'].append(("Bot",chunk.text))
|
| 44 |
+
#st.write("Bot:", bot_response)
|
| 45 |
+
else:
|
| 46 |
+
st.warning("Please enter something.")
|
| 47 |
+
# Function to interact with OpenAI API and get response
|
| 48 |
+
for role,text in st.session_state['chat_history']:
|
| 49 |
+
st.write(f"{role}:{text}")
|
| 50 |
+
|