-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
86 lines (73 loc) · 3.25 KB
/
Copy pathapp.py
File metadata and controls
86 lines (73 loc) · 3.25 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
# For Google GenAI SDK (Image Generation)
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import os
# Initialize LangChain chat model for text
llm_text = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
# Initialize Google GenAI client for image generation
API_KEY = os.getenv("GOOGLE_API_KEY")
client = genai.Client(api_key=API_KEY)
# Session state for chat history
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
st.title("🧠 Gemini Chatbot with Image Generation")
# Chat input
prompt = st.chat_input("Say something or request an image...")
if prompt:
st.session_state.chat_history.append({"role": "user", "content": prompt})
if "generate image:" in prompt.lower():
image_prompt = prompt.lower().replace("generate image:", "").strip()
with st.spinner("Generating image..."):
try:
response = client.models.generate_content(
model="gemini-2.0-flash-preview-image-generation",
contents=image_prompt,
config=types.GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"]
)
)
text_response = ""
image_data = None
for part in response.candidates[0].content.parts:
if part.text:
text_response += part.text
elif part.inline_data:
image = Image.open(BytesIO(part.inline_data.data))
image_data = image
# Store only text response in history, not image data
st.session_state.chat_history.append(
{"role": "assistant", "content": text_response, "is_image": True, "image_prompt": image_prompt}
)
# Display image immediately without storing in history
st.session_state.chat_history.append({"role": "assistant", "image_data": image_data})
except Exception as e:
st.session_state.chat_history.append(
{"role": "assistant", "content": f"Image generation failed: {str(e)}"}
)
else:
with st.spinner("Thinking..."):
try:
response = llm_text.invoke(
[HumanMessage(content=prompt)]
)
st.session_state.chat_history.append(
{"role": "assistant", "content": response.content}
)
except Exception as e:
st.session_state.chat_history.append(
{"role": "assistant", "content": f"Chat generation failed: {str(e)}"}
)
# Display chat history
for msg in st.session_state.chat_history:
if msg["role"] == "user":
st.chat_message("user").write(msg["content"])
elif msg["role"] == "assistant":
if "image_data" in msg:
st.chat_message("assistant").image(msg["image_data"], caption="Generated Image")
else:
st.chat_message("assistant").write(msg["content"])