eaglelandsonce commited on
Commit
77ffb52
·
verified ·
1 Parent(s): d6601af

Create 7_GeminiSystemPrompt.py

Browse files
Files changed (1) hide show
  1. pages/7_GeminiSystemPrompt.py +103 -0
pages/7_GeminiSystemPrompt.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import uuid
3
+ from gtts import gTTS
4
+ import google.generativeai as genai
5
+ from io import BytesIO # Import BytesIO
6
+
7
+ # Set your API key
8
+ api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace with your actual API key
9
+
10
+ genai.configure(api_key=api_key)
11
+
12
+ # Configure the generative AI model
13
+ generation_config = genai.GenerationConfig(
14
+ temperature=0.9,
15
+ max_output_tokens=3000
16
+ )
17
+
18
+ # Safety settings configuration
19
+ safety_settings = [
20
+ {
21
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
22
+ "threshold": "BLOCK_NONE",
23
+ },
24
+ {
25
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
26
+ "threshold": "BLOCK_NONE",
27
+ },
28
+ {
29
+ "category": "HARM_CATEGORY_HATE_SPEECH",
30
+ "threshold": "BLOCK_NONE",
31
+ },
32
+ {
33
+ "category": "HARM_CATEGORY_HARASSMENT",
34
+ "threshold": "BLOCK_NONE",
35
+ },
36
+ ]
37
+
38
+ # Initialize session state for chat history
39
+ if 'chat_history' not in st.session_state:
40
+ st.session_state['chat_history'] = []
41
+
42
+ st.title("Gemini Chatbot")
43
+
44
+ # Display chat history
45
+ def display_chat_history():
46
+ for entry in st.session_state['chat_history']:
47
+ st.markdown(f"{entry['role'].title()}: {entry['parts'][0]['text']}")
48
+
49
+ # Function to clear conversation history
50
+ def clear_conversation():
51
+ st.session_state['chat_history'] = []
52
+
53
+ # Send message function with system prompt
54
+ def send_message():
55
+ user_input = st.session_state.user_input
56
+ if user_input:
57
+ # Define a system prompt that provides context for the model's generation
58
+ system_prompt = "AI Planner System Prompt: As the AI Planner, your primary task is to assist in the development of a coherent and engaging book. You will be responsible for organizing the overall structure, defining the plot or narrative, and outlining the chapters or sections. To accomplish this, you will need to use your understanding of storytelling principles and genre conventions, as well as any specific information provided by the user, to create a well-structured framework for the book."
59
+
60
+ prompts = [entry['parts'][0]['text'] for entry in st.session_state['chat_history']]
61
+ prompts.append(user_input)
62
+
63
+ # Combine the system prompt with the chat history
64
+ chat_history_str = system_prompt + "\n" + "\n".join(prompts)
65
+
66
+ model = genai.GenerativeModel(
67
+ model_name='gemini-pro',
68
+ generation_config=generation_config,
69
+ safety_settings=safety_settings
70
+ )
71
+
72
+ response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
73
+ response_text = response.text if hasattr(response, "text") else "No response text found."
74
+
75
+ if response_text:
76
+ st.session_state['chat_history'].append({"role": "model", "parts":[{"text": response_text}]})
77
+
78
+ # Convert the response text to speech
79
+ tts = gTTS(text=response_text, lang='en')
80
+ tts_file = BytesIO()
81
+ tts.write_to_fp(tts_file)
82
+ tts_file.seek(0)
83
+ st.audio(tts_file, format='audio/mp3')
84
+
85
+ st.session_state.user_input = ''
86
+
87
+ display_chat_history()
88
+
89
+ # User input text area
90
+ user_input = st.text_area(
91
+ "Enter your message here:",
92
+ value="",
93
+ key="user_input"
94
+ )
95
+
96
+ # Send message button
97
+ send_button = st.button(
98
+ "Send",
99
+ on_click=send_message
100
+ )
101
+
102
+ # Clear conversation button
103
+ clear_button = st.button("Clear Conversation", on_click=clear_conversation)