ziyadsuper2017 commited on
Commit
b118f5f
·
1 Parent(s): 5cc2255

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -42
app.py CHANGED
@@ -1,80 +1,87 @@
1
  import streamlit as st
2
  import google.generativeai as genai
3
 
4
- # Configure API key
5
- genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
6
 
7
  # Model settings
8
  generation_config = {
9
  "temperature": 0.9,
10
  "top_p": 1,
11
- "top_k": 1,
12
- "max_output_tokens": 2048
13
  }
14
 
15
  safety_settings = [
16
- {
17
- "category": "HARM_CATEGORY_HARASSMENT",
18
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
19
- },
20
- {
21
- "category": "HARM_CATEGORY_HATE_SPEECH",
22
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
23
- },
24
- {
25
- "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
26
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
27
- },
28
- {
29
- "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
30
- "threshold": "BLOCK_MEDIUM_AND_ABOVE"
31
- }
32
  ]
33
 
34
  model = genai.GenerativeModel(
35
- model_name="gemini-pro",
36
- generation_config=generation_config,
37
- safety_settings=safety_settings
38
  )
39
 
40
  # Chatbot interface
41
- st.title("Gemini API Chatbot")
42
 
43
- chat_history = st.session_state.get("chat_history", [])
44
 
45
- chat_container = st.container()
46
 
47
  for message in chat_history:
48
  # Display message
49
- role = message["role"]
50
- content = message["content"]
 
 
 
 
51
 
52
  with chat_container.empty():
53
- st.markdown(f"**{role}:** {content}")
54
 
55
- # Get user input
56
- user_input = st.text_input("Ask the assistant")
57
 
58
  if user_input:
59
 
60
- # Process input
61
- chat_history.append({"role": "user", "content": user_input})
62
-
63
- # Display input
 
 
 
 
 
 
64
  with chat_container.empty():
65
  st.markdown(f"**user:** {user_input}")
66
 
67
  # Get response
68
  with st.spinner("Thinking..."):
69
- convo = model.start_chat(history=chat_history)
70
- response = convo.last.text
71
 
72
- # Process response
73
- chat_history.append({"role": "assistant", "content": response})
74
 
75
- # Display response
 
 
 
 
 
 
 
 
 
76
  with chat_container.empty():
77
- st.markdown(f"**assistant:** {response}")
78
-
79
  # Update session state
80
  st.session_state["chat_history"] = chat_history
 
1
  import streamlit as st
2
  import google.generativeai as genai
3
 
4
+ # API key
5
+ genai.configure(api_key="AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM")
6
 
7
  # Model settings
8
  generation_config = {
9
  "temperature": 0.9,
10
  "top_p": 1,
11
+ "top_k": 1,
12
+ "max_output_tokens": 2048
13
  }
14
 
15
  safety_settings = [
16
+ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
17
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
18
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
19
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}
 
 
 
 
 
 
 
 
 
 
 
 
20
  ]
21
 
22
  model = genai.GenerativeModel(
23
+ model_name="gemini-pro",
24
+ generation_config=generation_config,
25
+ safety_settings=safety_settings
26
  )
27
 
28
  # Chatbot interface
29
+ st.title("Gemini API Chatbot")
30
 
31
+ chat_history = st.session_state.get("chat_history", [])
32
 
33
+ chat_container = st.container()
34
 
35
  for message in chat_history:
36
  # Display message
37
+ if message.parts:
38
+ role = "assistant" if message.role == "system" else "user"
39
+ text = message.parts[0].text
40
+ else:
41
+ role = message.role
42
+ text = message.content
43
 
44
  with chat_container.empty():
45
+ st.markdown(f"**{role}:** {text}")
46
 
47
+ # Input
48
+ user_input = st.text_input("You")
49
 
50
  if user_input:
51
 
52
+ # Create message
53
+ user_message = genai.Content(
54
+ parts=[genai.Part(text=user_input)],
55
+ role=genai.Role.USER
56
+ )
57
+
58
+ # Append message
59
+ chat_history.append(user_message)
60
+
61
+ # Display message
62
  with chat_container.empty():
63
  st.markdown(f"**user:** {user_input}")
64
 
65
  # Get response
66
  with st.spinner("Thinking..."):
67
+ convo = model.start_chat(chat_history)
68
+ response = convo.last
69
 
70
+ # Extract response text
71
+ response_text = response.parts[0].text
72
 
73
+ # Create response message
74
+ assistant_message = genai.Content(
75
+ parts=[genai.Part(text=response_text)],
76
+ role=genai.Role.ASSISTANT
77
+ )
78
+
79
+ # Append response
80
+ chat_history.append(assistant_message)
81
+
82
+ # Display response
83
  with chat_container.empty():
84
+ st.markdown(f"**assistant:** {response_text}")
85
+
86
  # Update session state
87
  st.session_state["chat_history"] = chat_history