test12thjan / app.py
decodingdatascience's picture
Upload 2 files
352a497 verified
from dotenv import load_dotenv
load_dotenv()
import streamlit as st
import os
import pathlib
import google.generativeai as genai
os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# function to los the genini api model and get responses
def get_gemini_response(question):
model =genai.GenerativeModel('gemini-pro')
response = model.generate_content(question)
return response.text
#intizlize the streamlitapp
import streamlit as st
# Display the header
st.set_page_config(page_title="DDS Q & A Demo")
# Display the header
st.header("DDS Q & A Demo using Gemini")
# Initialize session state variables if they don't exist
if 'input_text' not in st.session_state:
st.session_state['input_text'] = ''
if 'clear_field' not in st.session_state:
st.session_state['clear_field'] = False
# Define a function to clear the input field
def clear_input():
st.session_state['input_text'] = ''
st.session_state['clear_field'] = True
# Display the input field
input_text = st.text_input("Input:", value='' if st.session_state['clear_field'] else st.session_state['input_text'], on_change=lambda: update_input_text(), key="input")
# Function to update session state with the current input text
def update_input_text():
st.session_state['input_text'] = st.session_state.input
st.session_state['clear_field'] = False
# Submit button
submit = st.button("Ask your Question")
# Clear button
if st.button("Clear", on_click=clear_input):
pass # The clear_input function is called when this button is pressed
# Logic to get response
if submit:
response = get_gemini_response(input_text)
st.subheader("The Response is")
st.write(response)