azy54 commited on
Commit
6148afa
·
verified ·
1 Parent(s): 03b40df

update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -5
app.py CHANGED
@@ -1,7 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
- import random
3
- import time
4
  import google.generativeai as genai
 
 
5
 
6
  # API Key
7
  GOOGLE_API_KEY = 'AIzaSyDnvz-l-Hq_tNkRkx2j_W8pd41XT08nH6Y'
@@ -10,6 +69,30 @@ genai.configure(api_key=GOOGLE_API_KEY)
10
  # Create a model object
11
  model = genai.GenerativeModel('gemini-pro')
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Streamlit app layout
14
  def main():
15
  st.title("Prompt Pilot")
@@ -33,7 +116,7 @@ def main():
33
 
34
  # Add user message to chat history
35
  st.session_state.messages.append({"role": "user", "content": prompt})
36
-
37
  # Generate the query for the Gemini model
38
  query = f"You are a prompt engineering assistant. Your task is to transform a raw prompt into an enhanced format, specifically using either RTF (Role Task Format) or RISE (Role Input Steps Expectations), as specified. The output should follow the {prompt_format} structure, and must not include any answers, hints, or suggestions, choose the role efficiently and critically related to that field. The goal is to prepare a prompt that, when used by a generative model, will produce the most accurate and relevant response. Here is the user's raw prompt: '{prompt}'. Please convert it into the {prompt_format} format."
39
 
@@ -43,13 +126,17 @@ def main():
43
 
44
  # Generate response using the Gemini model
45
  assistant_response = model.generate_content(query)
 
46
 
47
  # Display assistant response in chat message container
48
  with st.chat_message("assistant"):
49
- st.markdown(assistant_response.text)
50
 
51
  # Add assistant response to chat history
52
- st.session_state.messages.append({"role": "assistant", "content": assistant_response.text})
 
 
 
53
 
54
  if __name__ == "__main__":
55
  main()
 
1
+ # import streamlit as st
2
+ # import random
3
+ # import time
4
+ # import google.generativeai as genai
5
+
6
+ # # API Key
7
+ # GOOGLE_API_KEY = 'AIzaSyDnvz-l-Hq_tNkRkx2j_W8pd41XT08nH6Y'
8
+ # genai.configure(api_key=GOOGLE_API_KEY)
9
+
10
+ # # Create a model object
11
+ # model = genai.GenerativeModel('gemini-pro')
12
+
13
+ # # Streamlit app layout
14
+ # def main():
15
+ # st.title("Prompt Pilot")
16
+
17
+ # # Sidebar for prompt format selection
18
+ # st.sidebar.title("Select Prompt Format")
19
+ # prompt_format = st.sidebar.radio("Options", ("RISE", "RTF"))
20
+ # st.sidebar.write(f"Selected Format: {prompt_format}")
21
+
22
+ # # Chat section
23
+ # st.header("Chat Section")
24
+
25
+ # # Initialize chat history
26
+ # if "messages" not in st.session_state:
27
+ # st.session_state.messages = []
28
+
29
+ # # Accept user input
30
+ # if prompt := st.chat_input("Enter your prompt:"):
31
+ # # Clear previous messages
32
+ # st.session_state.messages = []
33
+
34
+ # # Add user message to chat history
35
+ # st.session_state.messages.append({"role": "user", "content": prompt})
36
+
37
+ # # Generate the query for the Gemini model
38
+ # query = f"You are a prompt engineering assistant. Your task is to transform a raw prompt into an enhanced format, specifically using either RTF (Role Task Format) or RISE (Role Input Steps Expectations), as specified. The output should follow the {prompt_format} structure, and must not include any answers, hints, or suggestions, choose the role efficiently and critically related to that field. The goal is to prepare a prompt that, when used by a generative model, will produce the most accurate and relevant response. Here is the user's raw prompt: '{prompt}'. Please convert it into the {prompt_format} format."
39
+
40
+ # # Display user message in chat message container
41
+ # with st.chat_message("user"):
42
+ # st.markdown(prompt)
43
+
44
+ # # Generate response using the Gemini model
45
+ # assistant_response = model.generate_content(query)
46
+
47
+ # # Display assistant response in chat message container
48
+ # with st.chat_message("assistant"):
49
+ # st.markdown(assistant_response.text)
50
+
51
+ # # Add assistant response to chat history
52
+ # st.session_state.messages.append({"role": "assistant", "content": assistant_response.text})
53
+
54
+ # if __name__ == "__main__":
55
+ # main()
56
+
57
+
58
+
59
+
60
  import streamlit as st
 
 
61
  import google.generativeai as genai
62
+ import json
63
+ import os
64
 
65
  # API Key
66
  GOOGLE_API_KEY = 'AIzaSyDnvz-l-Hq_tNkRkx2j_W8pd41XT08nH6Y'
 
69
  # Create a model object
70
  model = genai.GenerativeModel('gemini-pro')
71
 
72
+ # Function to save prompts to a JSON file
73
+ def save_prompts_to_json(prompt_format, raw_prompt, model_prompt):
74
+ # Save only if the selected format matches
75
+ data = {
76
+ "raw_prompt": raw_prompt,
77
+ "model_prompt": model_prompt,
78
+ "prompt_format": prompt_format
79
+ }
80
+
81
+ # If file exists, append to it; otherwise, create a new file
82
+ file_path = '/content/prompts.json' # Adjusted to save in the current directory
83
+ if os.path.exists(file_path):
84
+ with open(file_path, 'r+') as file:
85
+ # Load existing data
86
+ existing_data = json.load(file)
87
+ # Append new data
88
+ existing_data.append(data)
89
+ # Write updated data back to the file
90
+ file.seek(0)
91
+ json.dump(existing_data, file, indent=4)
92
+ else:
93
+ with open(file_path, 'w') as file:
94
+ json.dump([data], file, indent=4)
95
+
96
  # Streamlit app layout
97
  def main():
98
  st.title("Prompt Pilot")
 
116
 
117
  # Add user message to chat history
118
  st.session_state.messages.append({"role": "user", "content": prompt})
119
+
120
  # Generate the query for the Gemini model
121
  query = f"You are a prompt engineering assistant. Your task is to transform a raw prompt into an enhanced format, specifically using either RTF (Role Task Format) or RISE (Role Input Steps Expectations), as specified. The output should follow the {prompt_format} structure, and must not include any answers, hints, or suggestions, choose the role efficiently and critically related to that field. The goal is to prepare a prompt that, when used by a generative model, will produce the most accurate and relevant response. Here is the user's raw prompt: '{prompt}'. Please convert it into the {prompt_format} format."
122
 
 
126
 
127
  # Generate response using the Gemini model
128
  assistant_response = model.generate_content(query)
129
+ model_prompt = assistant_response.candidates[0].content.parts[0].text
130
 
131
  # Display assistant response in chat message container
132
  with st.chat_message("assistant"):
133
+ st.markdown(model_prompt)
134
 
135
  # Add assistant response to chat history
136
+ st.session_state.messages.append({"role": "assistant", "content": model_prompt})
137
+
138
+ # Save raw prompt and model-generated prompt to a JSON file in the selected format
139
+ save_prompts_to_json(prompt_format, prompt, model_prompt)
140
 
141
  if __name__ == "__main__":
142
  main()