Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import google.generativeai as genai
|
4 |
+
|
5 |
+
|
6 |
+
# Set up the Streamlit App
|
7 |
+
st.set_page_config(page_title="Chatbot with Gemini Flash", layout="wide")
|
8 |
+
st.title("Chatbot with Gemini Flash ⚡️")
|
9 |
+
st.caption("Chat with Google's Gemini Flash model using text input 🌟")
|
10 |
+
|
11 |
+
# Get OpenAI API key from user
|
12 |
+
api_key = st.text_input("Enter Google API Key", type="password")
|
13 |
+
|
14 |
+
# Set up the Gemini model
|
15 |
+
genai.configure(api_key=api_key)
|
16 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-flash-latest")
|
17 |
+
|
18 |
+
if api_key:
|
19 |
+
# Initialize the chat history
|
20 |
+
if "messages" not in st.session_state:
|
21 |
+
st.session_state.messages = []
|
22 |
+
|
23 |
+
# Main layout
|
24 |
+
chat_placeholder = st.container()
|
25 |
+
|
26 |
+
with chat_placeholder:
|
27 |
+
# Display the chat history
|
28 |
+
for message in st.session_state.messages:
|
29 |
+
with st.chat_message(message["role"]):
|
30 |
+
st.markdown(message["content"])
|
31 |
+
|
32 |
+
# User input area at the bottom
|
33 |
+
prompt = st.chat_input("What do you want to know?")
|
34 |
+
|
35 |
+
if prompt:
|
36 |
+
inputs = [prompt]
|
37 |
+
|
38 |
+
# Add user message to chat history
|
39 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
40 |
+
# Display user message in chat message container
|
41 |
+
with chat_placeholder:
|
42 |
+
with st.chat_message("user"):
|
43 |
+
st.markdown(prompt)
|
44 |
+
|
45 |
+
|
46 |
+
with st.spinner('Generating response...'):
|
47 |
+
# Generate response
|
48 |
+
response = model.generate_content(inputs)
|
49 |
+
|
50 |
+
# Display assistant response in chat message container
|
51 |
+
with chat_placeholder:
|
52 |
+
with st.chat_message("assistant"):
|
53 |
+
st.markdown(response.text)
|
54 |
+
|
55 |
+
if not prompt:
|
56 |
+
st.warning("Please enter a text query.")
|