ziyadsuper2017 commited on
Commit
fc87a94
·
1 Parent(s): 422431c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -103
app.py CHANGED
@@ -1,118 +1,59 @@
1
- import os
2
  import streamlit as st
3
- import google.generativeai as genai
4
  import sqlite3
5
 
6
- # Connect to SQLite database
7
  conn = sqlite3.connect('chat_history.db')
8
  c = conn.cursor()
9
 
10
- # Create table if it doesn't exist
11
  c.execute('''
12
- CREATE TABLE IF NOT EXISTS history (
13
- role TEXT,
14
- message TEXT
15
- )
16
- ''')
17
-
18
- # API key
19
  api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
20
-
21
- # Configure the API key
22
  genai.configure(api_key=api_key)
23
 
24
- # Set up the model
25
  generation_config = {
26
- "temperature": 0.9,
27
- "top_p": 1,
28
- "top_k": 1,
29
- "max_output_tokens": 2048,
30
  }
31
 
32
- safety_settings = [
33
- {
34
- "category": "HARM_CATEGORY_HARASSMENT",
35
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
36
- },
37
- {
38
- "category": "HARM_CATEGORY_HATE_SPEECH",
39
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
40
- },
41
- {
42
- "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
43
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
44
- },
45
- {
46
- "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
47
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
48
- }
49
- ]
50
-
51
- model = genai.GenerativeModel(model_name="gemini-pro",
52
- generation_config=generation_config,
53
- safety_settings=safety_settings)
54
-
55
- # Create chatbot interface
56
- st.title("Gemini API Chatbot")
57
-
58
- # Get chat history from session state
59
- chat_history = st.session_state.get("chat_history", [])
60
-
61
- # Convert chat history to the expected format
62
- chat_history = [{"role": message["role"], "parts": [{"text": message.get("content", "")}]} for message in chat_history]
63
-
64
- # Display previous messages
65
  for message in chat_history:
66
- role, text = message["role"], message["parts"][0]["text"]
67
- st.markdown(f"**{role.title()}:** {text}")
68
-
69
- # Get user input from text box
70
- user_input = st.text_input("You")
71
-
72
- # Check if user input is not empty
73
  if user_input:
74
- # Add user message to chat history
75
- chat_history.append({"role": "user", "parts": [{"text": user_input}]})
76
-
77
- # Display user message with markdown
78
- st.markdown(f"**You:** {user_input}")
79
-
80
- # Get model response with generate_content method
81
- with st.spinner("Thinking..."):
82
- response = model.generate_content(chat_history)
83
-
84
- # Get response text from response object
85
- response_text = response.text
86
-
87
- # Add response message to chat history
88
- chat_history.append({"role": "assistant", "parts": [{"text": response_text}]})
89
-
90
- # Display response message with markdown
91
- st.markdown(f"**Gemini Bot:** {response_text}")
92
-
93
- # Update session state with chat history
94
- st.session_state["chat_history"] = chat_history
95
-
96
- # Add a button to reset chat
97
- if st.button("Reset Chat"):
98
- st.session_state["chat_history"] = []
99
-
100
- # Add a button to display chat history from database
101
- if st.button("Display History"):
102
- c.execute("SELECT * FROM history")
103
- rows = c.fetchall()
104
- for row in rows:
105
- st.markdown(f"**{row[0].title()}:** {row[1]}")
106
-
107
- # Add a button to clear chat history from database
108
- if st.button("Clear History"):
109
- c.execute("DELETE FROM history")
110
- conn.commit()
111
-
112
- # Save chat history to database
113
- for message in chat_history:
114
- c.execute("INSERT INTO history VALUES (?, ?)", (message["role"], message["parts"][0]["text"]))
115
- conn.commit()
116
-
117
- # Close the connection
118
- conn.close()
 
 
1
  import streamlit as st
2
+ import google.generativeai as genai
3
  import sqlite3
4
 
 
5
  conn = sqlite3.connect('chat_history.db')
6
  c = conn.cursor()
7
 
 
8
  c.execute('''
9
+ CREATE TABLE IF NOT EXISTS history
10
+ (role TEXT, message TEXT)
11
+ ''')
12
+
 
 
 
13
  api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM"
 
 
14
  genai.configure(api_key=api_key)
15
 
 
16
  generation_config = {
17
+ "temperature": 0.9,
18
+ "max_output_tokens": 100,
 
 
19
  }
20
 
21
+ safety_settings = []
22
+
23
+ model = genai.GenerativeModel(
24
+ model_name="gemini",
25
+ generation_config=generation_config,
26
+ safety_settings=safety_settings
27
+ )
28
+
29
+ st.title("Chatbot")
30
+
31
+ chat_history = st.session_state.get("chat_history", [])
32
+
33
+ if len(chat_history) % 2 == 0:
34
+ role = "user"
35
+ else:
36
+ role = "model"
37
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  for message in chat_history:
39
+ r, t = message["role"], message["parts"][0]["text"]
40
+ st.markdown(f"**{r.title()}:** {t}")
41
+
42
+ user_input = st.text_input("")
43
+
 
 
44
  if user_input:
45
+ chat_history.append({"role": role, "parts": [{"text": user_input}]})
46
+
47
+ if role == "user":
48
+ response = model.generate_content(chat_history)
49
+ response_text = response.text
50
+
51
+ chat_history.append({"role": "model", "parts": [{"text": response_text}]})
52
+
53
+ st.session_state["chat_history"] = chat_history
54
+
55
+ for message in chat_history:
56
+ r, t = message["role"], message["parts"][0]["text"]
57
+ st.markdown(f"**{r.title()}:** {t}")
58
+
59
+ # Save and display chat history