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

Create 8_Gemini-doublequery.py

Browse files
Files changed (1) hide show
  1. pages/8_Gemini-doublequery.py +114 -0
pages/8_Gemini-doublequery.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from gtts import gTTS
3
+ import google.generativeai as genai
4
+ from io import BytesIO
5
+
6
+ # Set your API key
7
+ api_key = "your_actual_api_key_here" # Replace with your actual API key
8
+
9
+ genai.configure(api_key=api_key)
10
+
11
+ # Configure the generative AI model
12
+ generation_config = genai.GenerationConfig(
13
+ temperature=0.9,
14
+ max_output_tokens=3000
15
+ )
16
+
17
+ # Safety settings configuration
18
+ safety_settings = [
19
+ {
20
+ "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
21
+ "threshold": "BLOCK_NONE",
22
+ },
23
+ {
24
+ "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
25
+ "threshold": "BLOCK_NONE",
26
+ },
27
+ {
28
+ "category": "HARM_CATEGORY_HATE_SPEECH",
29
+ "threshold": "BLOCK_NONE",
30
+ },
31
+ {
32
+ "category": "HARM_CATEGORY_HARASSMENT",
33
+ "threshold": "BLOCK_NONE",
34
+ },
35
+ ]
36
+
37
+ # Initialize session state for chat history
38
+ if 'chat_history' not in st.session_state:
39
+ st.session_state['chat_history'] = []
40
+
41
+ st.title("Gemini Chatbot")
42
+
43
+ # Display chat history
44
+ def display_chat_history():
45
+ for entry in st.session_state['chat_history']:
46
+ st.markdown(f"{entry['role'].title()}: {entry['parts'][0]['text']}")
47
+
48
+ # Function to clear conversation history
49
+ def clear_conversation():
50
+ st.session_state['chat_history'] = []
51
+
52
+ # Send message function with sequential AI model interaction
53
+ def send_message():
54
+ user_input = st.session_state.user_input
55
+ if user_input:
56
+ # Initial system prompt for the chatbot interaction
57
+ initial_system_prompt = "You are a knowledgeable and helpful chatbot. Respond to the user queries informatively and politely."
58
+
59
+ # AI Writer System Prompt for generating text based on the outline
60
+ ai_writer_system_prompt = "As the AI Writer, your main objective is to generate the actual text of the book based on the outline provided by the AI Planner. You will use natural language generation techniques to produce coherent and readable prose that follows the structure and narrative defined by the AI Planner. Your output should adhere to the user's style and tone preferences, and you should incorporate any specific information or prompts provided by the user to create a captivating and immersive story."
61
+
62
+ prompts = [entry['parts'][0]['text'] for entry in st.session_state['chat_history']]
63
+ prompts.append(user_input)
64
+
65
+ # Combine initial system prompt with the chat history
66
+ chat_history_str = initial_system_prompt + "\n" + "\n".join(prompts)
67
+
68
+ model = genai.GenerativeModel(
69
+ model_name='gemini-pro',
70
+ generation_config=generation_config,
71
+ safety_settings=safety_settings
72
+ )
73
+
74
+ # First model generation call
75
+ initial_response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
76
+ initial_response_text = initial_response.text if hasattr(initial_response, "text") else "No response text found."
77
+
78
+ if initial_response_text:
79
+ # Use the output of the first model call as input for the second, applying the AI Writer System Prompt
80
+ final_chat_history_str = ai_writer_system_prompt + "\n" + initial_response_text
81
+
82
+ # Second model generation call
83
+ final_response = model.generate_content([{"role": "model", "parts": [{"text": final_chat_history_str}]}])
84
+ final_response_text = final_response.text if hasattr(final_response, "text") else "No response text found."
85
+
86
+ if final_response_text:
87
+ st.session_state['chat_history'].append({"role": "model", "parts":[{"text": final_response_text}]})
88
+
89
+ # Convert the final response text to speech
90
+ tts = gTTS(text=final_response_text, lang='en')
91
+ tts_file = BytesIO()
92
+ tts.write_to_fp(tts_file)
93
+ tts_file.seek(0)
94
+ st.audio(tts_file, format='audio/mp3')
95
+
96
+ st.session_state.user_input = ''
97
+
98
+ display_chat_history()
99
+
100
+ # User input text area
101
+ user_input = st.text_area(
102
+ "Enter your message here:",
103
+ value="",
104
+ key="user_input"
105
+ )
106
+
107
+ # Send message button
108
+ send_button = st.button(
109
+ "Send",
110
+ on_click=send_message
111
+ )
112
+
113
+ # Clear conversation button
114
+ clear_button = st.button("Clear Conversation", on_click=clear_conversation)