ziyadsuper2017 commited on
Commit
bd1d195
·
1 Parent(s): 2ae0133

Update app.py

Browse files

Trying to update a bug in the reset button

Files changed (1) hide show
  1. app.py +71 -87
app.py CHANGED
@@ -1,129 +1,113 @@
1
  import streamlit as st
2
- import google.generativeai as genai
3
  import sqlite3
4
  from streamlit import file_uploader
5
 
6
  # Database setup
7
- conn = sqlite3.connect('chat_history.db')
8
- c = conn.cursor()
9
 
10
  c.execute('''
11
- CREATE TABLE IF NOT EXISTS history
12
- (role TEXT, message TEXT)
13
  ''')
14
 
15
  # Generative AI setup
16
- api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
17
  genai.configure(api_key=api_key)
18
 
19
  generation_config = {
20
  "temperature": 0.9,
21
- "max_output_tokens": 500
22
  }
23
 
24
- safety_settings = []
25
 
26
  # Streamlit UI
27
- st.title("Chatbot")
28
 
29
  chat_history = st.session_state.get("chat_history", [])
30
 
31
- if len(chat_history) % 2 == 0:
32
- role = "user"
33
- else:
34
- role = "model"
35
 
36
- for message in chat_history:
37
- r, t = message["role"], message["parts"][0]["text"]
38
- st.markdown(f"**{r.title()}:** {t}")
39
-
40
- user_input = st.text_input("")
41
-
42
- if user_input:
43
- chat_history.append({"role": role, "parts": [{"text": user_input}]})
44
-
45
- if role == "user":
46
- # If only text is entered, follow the previous code
47
- model_name = "gemini-pro"
48
-
49
- model = genai.GenerativeModel(
50
- model_name=model_name,
51
- generation_config=generation_config,
52
- safety_settings=safety_settings
53
- )
54
-
55
- response = model.generate_content(chat_history)
56
- response_text = response.text
57
- chat_history.append({"role": "model", "parts": [{"text": response_text}]})
58
-
59
- st.session_state["chat_history"] = chat_history
60
-
61
- for message in chat_history:
62
  r, t = message["role"], message["parts"][0]["text"]
63
- st.markdown(f"**{r.title()}:** {t}")
64
-
65
- if st.button("Display History"):
66
- c.execute("SELECT * FROM history")
67
- rows = c.fetchall()
68
-
69
- for row in rows:
70
- st.markdown(f"**{row[0].title()}:** {row[1]}")
 
 
 
 
 
 
71
 
72
- # Save chat history to database
73
- for message in chat_history:
74
- c.execute("INSERT INTO history VALUES (?, ?)",
75
- (message["role"], message["parts"][0]["text"]))
76
- conn.commit()
 
 
 
 
77
 
78
- # Create a placeholder for the history display
 
79
  history_placeholder = st.empty()
80
 
81
  # Use the key argument for the buttons
82
- if st.button("Display History", key="display"):
83
- c.execute("SELECT * FROM history")
84
- rows = c.fetchall()
85
-
86
- # Fill the placeholder with the history
87
- for row in rows:
88
- history_placeholder.markdown(f"**{row[0].title()}:** {row[1]}")
89
 
 
 
 
 
90
  # Define a function to clear the placeholder
91
  def clear_history():
92
  history_placeholder.empty()
93
-
94
- # Use the key argument for the buttons
95
  if st.button("Reset History", key="reset", on_click=clear_history):
96
  pass
97
 
98
-
99
  conn.close()
100
 
101
  # Separate section for image uploading
102
- st.title("Image Description Generator")
103
-
104
- uploaded_file = st.file_uploader("Upload an image here", type=["png", "jpg", "jpeg"])
105
 
106
- # Text input for asking questions about the image
107
  image_question = st.text_input("Ask something about the image:")
108
 
109
  if uploaded_file and image_question:
110
- image_parts = [
111
- {
112
- "mime_type": uploaded_file.type,
113
- "data": uploaded_file.read()
114
- },
115
- ]
116
-
117
- prompt_parts = [
118
- image_question,
119
- image_parts[0],
120
- ]
121
-
122
- model = genai.GenerativeModel(
123
- model_name="gemini-pro-vision",
124
- generation_config=generation_config,
125
- safety_settings=safety_settings
126
- )
127
-
128
- response = model.generate_content(prompt_parts)
129
- st.markdown(f"**Model's answer:** {response.text}")
 
1
  import streamlit as st
2
+ import google.generativeai as genai
3
  import sqlite3
4
  from streamlit import file_uploader
5
 
6
  # Database setup
7
+ conn = sqlite3.connect('chat_history.db')
8
+ c = conn.cursor()
9
 
10
  c.execute('''
11
+ CREATE TABLE IF NOT EXISTS history
12
+ (role TEXT, message TEXT)
13
  ''')
14
 
15
  # Generative AI setup
16
+ api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
17
  genai.configure(api_key=api_key)
18
 
19
  generation_config = {
20
  "temperature": 0.9,
21
+ "max_output_tokens": 500
22
  }
23
 
24
+ safety_settings = []
25
 
26
  # Streamlit UI
27
+ st.title("Chatbot")
28
 
29
  chat_history = st.session_state.get("chat_history", [])
30
 
31
+ if len(chat_history) % 2 == 0:
32
+ role = "user"
33
+ else:
34
+ role = "model"
35
 
36
+ for message in chat_history:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  r, t = message["role"], message["parts"][0]["text"]
38
+ st.markdown(f"**{r.title()}:** {t}")
39
+
40
+ user_input = st.text_input("")
41
+ if user_input:
42
+ chat_history.append({"role": role, "parts": [{"text": user_input}]})
43
+ if role == "user":
44
+
45
+ # If only text is entered, follow the previous code
46
+ model_name = "gemini-pro"
47
+ model = genai.GenerativeModel(
48
+ model_name=model_name,
49
+ generation_config=generation_config,
50
+ safety_settings=safety_settings
51
+ )
52
 
53
+ response = model.generate_content(chat_history)
54
+ response_text = response.text
55
+ chat_history.append({"role": "model", "parts": [{"text": response_text}]})
56
+
57
+ st.session_state["chat_history"] = chat_history
58
+
59
+ for message in chat_history:
60
+ r, t = message["role"], message["parts"][0]["text"]
61
+ st.markdown(f"**{r.title()}:** {t}")
62
 
63
+
64
+ # Create a placeholder for the history display
65
  history_placeholder = st.empty()
66
 
67
  # Use the key argument for the buttons
68
+ if st.button("Display History", key="display"):
69
+ c.execute("SELECT * FROM history")
70
+ rows = c.fetchall()
 
 
 
 
71
 
72
+ # Fill the placeholder with the history
73
+ for row in rows:
74
+ history_placeholder.markdown(f"**{row[0].title()}:** {row[1]}")
75
+
76
  # Define a function to clear the placeholder
77
  def clear_history():
78
  history_placeholder.empty()
79
+
80
+ # Use the key argument for the buttons
81
  if st.button("Reset History", key="reset", on_click=clear_history):
82
  pass
83
 
 
84
  conn.close()
85
 
86
  # Separate section for image uploading
87
+ st.title("Image Description Generator")
88
+ uploaded_file = st.file_uploader("Upload an image here", type=["png", "jpg", "jpeg"])
 
89
 
90
+ # Text input for asking questions about the image:
91
  image_question = st.text_input("Ask something about the image:")
92
 
93
  if uploaded_file and image_question:
94
+ image_parts = [
95
+ {
96
+ "mime_type": uploaded_file.type,
97
+ "data": uploaded_file.read()
98
+ },
99
+ ]
100
+
101
+ prompt_parts = [
102
+ image_question,
103
+ image_parts[0],
104
+ ]
105
+
106
+ model = genai.GenerativeModel(
107
+ model_name="gemini-pro-vision",
108
+ generation_config=generation_config,
109
+ safety_settings=safety_settings
110
+ )
111
+
112
+ response = model.generate_content(prompt_parts)
113
+ st.markdown(f"**Model's answer:** {response.text}")