RChaubey16 commited on
Commit
fc6b047
·
verified ·
1 Parent(s): 6940750

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -30
app.py CHANGED
@@ -10,30 +10,45 @@ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
10
  # Initialize chat model
11
  model = genai.GenerativeModel("gemini-1.5-flash")
12
 
13
- # Streamlit UI
14
- st.title("🤖 AI Chatbot (Gemini 1.5 Flash)")
15
-
16
- # Add description
17
- st.markdown("""
18
- ### About this Chatbot
19
- This is an AI-powered chatbot built using:
20
- * **Gemini 1.5 Flash** - Google's latest language model
21
- * **Streamlit** - For the interactive web interface
22
- * **Python** - For backend implementation
23
 
24
- The chatbot can help you with:
25
- - General questions and conversations
26
- - Writing and analysis tasks
27
- - Problem-solving and explanations
28
- """)
 
 
 
 
 
 
 
 
 
29
 
30
- st.write("Ask me anything!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # Store chat history
33
  if "messages" not in st.session_state:
34
  st.session_state.messages = []
35
 
36
- # Display previous messages
37
  for msg in st.session_state.messages:
38
  with st.chat_message(msg["role"]):
39
  st.markdown(msg["content"])
@@ -44,29 +59,25 @@ user_input = st.chat_input("Type your message...")
44
  if user_input:
45
  # Display user message
46
  st.chat_message("user").markdown(user_input)
47
-
48
- # Prepare chat history for context
49
- chat_history = [
50
- {"role": "user" if m["role"] == "user" else "model", "parts": [m["content"]]}
51
- for m in st.session_state.messages
52
- ]
53
 
54
- # Call Gemini API
 
 
 
55
  response = model.generate_content(
56
  contents=[{"role": "user", "parts": [user_input]}],
57
  generation_config={"temperature": 0.7},
58
  safety_settings=[]
59
  )
60
-
61
  bot_reply = response.text
62
-
63
- # Display bot response
64
  st.chat_message("assistant").markdown(bot_reply)
65
-
66
  # Save conversation
67
  st.session_state.messages.append({"role": "user", "content": user_input})
68
  st.session_state.messages.append({"role": "assistant", "content": bot_reply})
69
 
70
- # Keep only last 3 message exchanges (6 messages total)
71
  if len(st.session_state.messages) > 6:
72
  st.session_state.messages = st.session_state.messages[-6:]
 
10
  # Initialize chat model
11
  model = genai.GenerativeModel("gemini-1.5-flash")
12
 
13
+ # Streamlit UI Design
14
+ st.set_page_config(page_title="AI Chatbot", page_icon="🤖", layout="centered")
 
 
 
 
 
 
 
 
15
 
16
+ st.markdown(
17
+ """
18
+ <style>
19
+ body {
20
+ background-color: #f5f7fa;
21
+ }
22
+ .stChatMessage {
23
+ border-radius: 12px;
24
+ padding: 12px;
25
+ }
26
+ </style>
27
+ """,
28
+ unsafe_allow_html=True
29
+ )
30
 
31
+ # Title and description
32
+ st.title("🤖 AI Chatbot (Gemini 1.5 Flash)")
33
+ st.markdown(
34
+ """
35
+ ### Welcome to Your AI Chatbot!
36
+ This chatbot is powered by **Google Gemini 1.5 Flash** and built with **Streamlit**.
37
+
38
+ 🧠 **What can it do?**
39
+ - Answer questions & provide explanations
40
+ - Help with writing & analysis tasks
41
+ - Assist with problem-solving
42
+
43
+ 🔥 Start chatting below!
44
+ """
45
+ )
46
 
47
+ # Initialize chat history
48
  if "messages" not in st.session_state:
49
  st.session_state.messages = []
50
 
51
+ # Display chat history
52
  for msg in st.session_state.messages:
53
  with st.chat_message(msg["role"]):
54
  st.markdown(msg["content"])
 
59
  if user_input:
60
  # Display user message
61
  st.chat_message("user").markdown(user_input)
 
 
 
 
 
 
62
 
63
+ # Format chat history for context
64
+ chat_history = [{"role": "user" if m["role"] == "user" else "model", "parts": [m["content"]]} for m in st.session_state.messages]
65
+
66
+ # Generate AI response
67
  response = model.generate_content(
68
  contents=[{"role": "user", "parts": [user_input]}],
69
  generation_config={"temperature": 0.7},
70
  safety_settings=[]
71
  )
 
72
  bot_reply = response.text
73
+
74
+ # Display AI response
75
  st.chat_message("assistant").markdown(bot_reply)
76
+
77
  # Save conversation
78
  st.session_state.messages.append({"role": "user", "content": user_input})
79
  st.session_state.messages.append({"role": "assistant", "content": bot_reply})
80
 
81
+ # Keep chat history concise
82
  if len(st.session_state.messages) > 6:
83
  st.session_state.messages = st.session_state.messages[-6:]