Ninjasharp commited on
Commit
69411dd
·
verified ·
1 Parent(s): 30b6592

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -158
app.py CHANGED
@@ -1,158 +1,158 @@
1
- import streamlit as st
2
- import os
3
- from groq import Groq
4
-
5
- # Streamlit page configuration
6
- st.set_page_config(layout="wide")
7
-
8
- # Supported models
9
- SUPPORTED_MODELS = {
10
- "Llama 3.2 1B (Preview)": "llama-3.2-1b-preview",
11
- "Llama 3 70B": "llama3-70b-8192",
12
- "Llama 3 8B": "llama3-8b-8192",
13
- "Llama 3.1 70B": "llama-3.1-70b-versatile",
14
- "Llama 3.1 8B": "llama-3.1-8b-instant",
15
- "Mixtral 8x7B": "mixtral-8x7b-32768",
16
- "Gemma 2 9B": "gemma2-9b-it",
17
- "LLaVA 1.5 7B": "llava-v1.5-7b-4096-preview",
18
- "Llama 3.2 3B (Preview)": "llama-3.2-3b-preview",
19
- "Llama 3.2 11B Vision (Preview)": "llama-3.2-11b-vision-preview"
20
- }
21
-
22
- MAX_TOKENS = 1000
23
-
24
- # Initialize Groq client with API key
25
- groq_api_key = os.getenv("GROQ_API_KEY")
26
- if not groq_api_key:
27
- st.error("GROQ_API_KEY not found in environment variables. Please set it and restart the app.")
28
- st.stop()
29
-
30
- client = Groq(api_key=groq_api_key)
31
- st.image("p1.png", width=300)
32
- st.sidebar.image("p2.png", width=200)
33
-
34
- def main():
35
- st.title("Marketing tool App")
36
-
37
- # Sidebar settings
38
- st.sidebar.header("Configuration")
39
- model = st.sidebar.selectbox("Select LLM Model", list(SUPPORTED_MODELS.keys()))
40
- temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.5)
41
- output_size = st.sidebar.selectbox(
42
- "Select Output Size",
43
- ["1-3 word sentences", "2-5 word sentences", "3-7 word sentences", "5-9 word sentences", "6-11 word sentences"]
44
- )
45
- bullet_points = st.sidebar.checkbox("Output as Bullet Points", value=True)
46
- humanize_text = st.sidebar.checkbox("Humanize Text")
47
- display_final_answer = st.sidebar.checkbox("Display Process", value=True)
48
- reduce_words = st.sidebar.checkbox("Reduce Word Count by 50%") # New checkbox for reducing word count
49
-
50
- # Clear and reset buttons in the sidebar
51
- if st.sidebar.button("Clear Input Fields"):
52
- st.session_state.system_prompt = "Create a revised [text] use 3-5 words concise and focused, Provide the output in short format plus in bullet points or a brief paragraph, plus offer 2-3 alternates - suggest areas for improvement. . list final answer in separate area"
53
- st.session_state.user_query = ""
54
-
55
- # Input fields for system prompt and query
56
- default_prompt = "Create a revised [text] use 3-5 words concise and focused, Provide the output in short format plus in bullet points or a brief paragraph, plus offer 2-3 alternates - suggest areas for improvement. . list final answer in separate area"
57
- system_prompt = st.text_area("System Prompt", value=st.session_state.get("system_prompt", default_prompt), key="system_prompt")
58
- user_query = st.text_area("Enter Your Query", value=st.session_state.get("user_query", ""), key="user_query")
59
-
60
- if st.button("Submit"):
61
- with st.spinner("Generating response..."):
62
- response = query_groq(model, temperature, system_prompt, user_query, output_size, humanize_text, reduce_words)
63
-
64
- col1, col2 = st.columns(2)
65
-
66
- with col1:
67
- st.write("### Detailed Information")
68
- st.write("Model:", model)
69
- st.write("Temperature:", temperature)
70
- st.write("Output Size:", output_size)
71
- st.write("Bullet Points:")
72
- st.write(bullet_points)
73
- st.write("Humanize Text:", humanize_text)
74
- st.write("Display Final Answer:", display_final_answer)
75
- st.write("System Prompt:", system_prompt)
76
- st.write("User Query:", user_query)
77
- if display_final_answer:
78
- st.write("### Original Response")
79
- st.text_area("Original Response", value=response, height=600)
80
-
81
- with col2:
82
- if display_final_answer:
83
- processed_response = process_response(response, output_size, bullet_points, humanize_text, reduce_words)
84
- additional_text = "Please review the response carefully before proceeding."
85
- st.write("### Processed Response with Review")
86
- st.text_area(response, value=processed_response + "\n" + additional_text, height=200)
87
- else:
88
- st.write("### Output Response")
89
- st.text(response)
90
-
91
- def query_groq(model, temperature, system_prompt, user_query, output_size, humanize_text, reduce_words):
92
- try:
93
- completion = client.chat.completions.create(
94
- model=SUPPORTED_MODELS[model],
95
- messages=[
96
- {"role": "system", "content": system_prompt},
97
- {"role": "user", "content": user_query}
98
- ],
99
- temperature=temperature,
100
- max_tokens=MAX_TOKENS
101
- )
102
- if not completion.choices:
103
- return "Error: No choices in the completion response."
104
- return completion.choices[0].message.content
105
- except Exception as e:
106
- return f"Error: {str(e)}"
107
-
108
- def process_response(text, output_size, bullet_points, humanize_text, reduce_words):
109
- if reduce_words:
110
- # Reduce word count by 50%
111
- words = text.split()
112
- text = " ".join(words[:len(words)//2])
113
-
114
- if output_size == "1-3 word sentences":
115
- text = reduce_to_sentences(text, 1, 3)
116
- elif output_size == "2-5 word sentences":
117
- text = reduce_to_sentences(text, 2, 5)
118
- elif output_size == "3-7 word sentences":
119
- text = reduce_to_sentences(text, 3, 7)
120
- elif output_size == "5-9 word sentences":
121
- text = reduce_to_sentences(text, 5, 9)
122
- elif output_size == "6-11 word sentences":
123
- text = reduce_to_sentences(text, 6, 11)
124
-
125
- if bullet_points:
126
- text = reduce_to_bullet_points(text, 1, 11)
127
-
128
- if humanize_text:
129
- text = humanize(text)
130
-
131
- return text
132
-
133
- def reduce_to_bullet_points(text, min_words, max_words):
134
- sentences = text.split('.')
135
- bullet_points = []
136
- for sentence in sentences:
137
- words = sentence.strip().split()
138
- if min_words <= len(words) <= max_words:
139
- bullet_points.append(f"- {' '.join(words)}")
140
- return '\n'.join(bullet_points)
141
-
142
- def reduce_to_sentences(text, min_words, max_words):
143
- sentences = text.split('.')
144
- filtered_sentences = []
145
- for sentence in sentences:
146
- words = sentence.strip().split()
147
- if min_words <= len(words) <= max_words:
148
- filtered_sentences.append(sentence.strip())
149
- return ' '.join(filtered_sentences)
150
-
151
- def humanize(text):
152
- # This can be replaced with a more sophisticated humanization logic as needed
153
- return text.replace(". ", ". Let's consider this further. ")
154
-
155
- st.sidebar.info("build by dw")
156
-
157
- if __name__ == "__main__":
158
- main()
 
1
+ import streamlit as st
2
+ import os
3
+ from groq import Groq
4
+
5
+ # Streamlit page configuration
6
+ st.set_page_config(layout="wide")
7
+
8
+ # Supported models
9
+ SUPPORTED_MODELS = {
10
+ "Llama 3.2 1B (Preview)": "llama-3.2-1b-preview",
11
+ "Llama 3 70B": "llama3-70b-8192",
12
+ "Llama 3 8B": "llama3-8b-8192",
13
+ "Llama 3.1 70B": "llama-3.1-70b-versatile",
14
+ "Llama 3.1 8B": "llama-3.1-8b-instant",
15
+ "Mixtral 8x7B": "mixtral-8x7b-32768",
16
+ "Gemma 2 9B": "gemma2-9b-it",
17
+ "LLaVA 1.5 7B": "llava-v1.5-7b-4096-preview",
18
+ "Llama 3.2 3B (Preview)": "llama-3.2-3b-preview",
19
+ "Llama 3.2 11B Vision (Preview)": "llama-3.2-11b-vision-preview"
20
+ }
21
+
22
+ MAX_TOKENS = 1000
23
+
24
+ # Initialize Groq client with API key
25
+ groq_api_key = os.getenv("GROQ_API_KEY")
26
+ if not groq_api_key:
27
+ st.error("GROQ_API_KEY not found in environment variables. Please set it and restart the app.")
28
+ st.stop()
29
+
30
+ client = Groq(api_key=groq_api_key)
31
+ st.image("p1.png", width=300)
32
+ st.sidebar.image("p2.png", width=200)
33
+
34
+ def main():
35
+ st.title("Marketing tool App")
36
+
37
+ # Sidebar settings
38
+ st.sidebar.header("Configuration")
39
+ model = st.sidebar.selectbox("Select LLM Model", list(SUPPORTED_MODELS.keys()))
40
+ temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.5)
41
+ output_size = st.sidebar.selectbox(
42
+ "Select Output Size",
43
+ ["1-3 word sentences", "2-5 word sentences", "3-7 word sentences", "5-9 word sentences", "6-11 word sentences"]
44
+ )
45
+ bullet_points = st.sidebar.checkbox("Output as Bullet Points", value=True)
46
+ humanize_text = st.sidebar.checkbox("Humanize Text")
47
+ display_final_answer = st.sidebar.checkbox("Display Process")
48
+ reduce_words = st.sidebar.checkbox("Reduce Word Count by 50%") # New checkbox for reducing word count
49
+
50
+ # Clear and reset buttons in the sidebar
51
+ if st.sidebar.button("Clear Input Fields"):
52
+ st.session_state.system_prompt = "Create a revised [text] use 3-5 words concise and focused, Provide the output in short format plus in bullet points or a brief paragraph, plus offer 2-3 alternates - suggest areas for improvement. . list final answer in separate area"
53
+ st.session_state.user_query = ""
54
+
55
+ # Input fields for system prompt and query
56
+ default_prompt = "Create a revised [text] use 3-5 words concise and focused, Provide the output in short format plus in bullet points or a brief paragraph, plus offer 2-3 alternates - suggest areas for improvement. . list final answer in separate area"
57
+ system_prompt = st.text_area("System Prompt", value=st.session_state.get("system_prompt", default_prompt), key="system_prompt")
58
+ user_query = st.text_area("Enter Your Query", value=st.session_state.get("user_query", ""), key="user_query")
59
+
60
+ if st.button("Submit"):
61
+ with st.spinner("Generating response..."):
62
+ response = query_groq(model, temperature, system_prompt, user_query, output_size, humanize_text, reduce_words)
63
+
64
+ col1, col2 = st.columns(2)
65
+
66
+ with col1:
67
+ st.write("### Detailed Information")
68
+ st.write("Model:", model)
69
+ st.write("Temperature:", temperature)
70
+ st.write("Output Size:", output_size)
71
+ st.write("Bullet Points:")
72
+ st.write(bullet_points)
73
+ st.write("Humanize Text:", humanize_text)
74
+ st.write("Display Final Answer:", display_final_answer)
75
+ st.write("System Prompt:", system_prompt)
76
+ st.write("User Query:", user_query)
77
+ if display_final_answer:
78
+ st.write("### Original Response")
79
+ st.text_area("Original Response", value=response, height=600)
80
+
81
+ with col2:
82
+ if display_final_answer:
83
+ processed_response = process_response(response, output_size, bullet_points, humanize_text, reduce_words)
84
+ additional_text = "Please review the response carefully before proceeding."
85
+ st.write("### Processed Response with Review")
86
+ st.text_area(response, value=processed_response + "\n" + additional_text, height=200)
87
+ else:
88
+ st.write("### Output Response")
89
+ st.text(response)
90
+
91
+ def query_groq(model, temperature, system_prompt, user_query, output_size, humanize_text, reduce_words):
92
+ try:
93
+ completion = client.chat.completions.create(
94
+ model=SUPPORTED_MODELS[model],
95
+ messages=[
96
+ {"role": "system", "content": system_prompt},
97
+ {"role": "user", "content": user_query}
98
+ ],
99
+ temperature=temperature,
100
+ max_tokens=MAX_TOKENS
101
+ )
102
+ if not completion.choices:
103
+ return "Error: No choices in the completion response."
104
+ return completion.choices[0].message.content
105
+ except Exception as e:
106
+ return f"Error: {str(e)}"
107
+
108
+ def process_response(text, output_size, bullet_points, humanize_text, reduce_words):
109
+ if reduce_words:
110
+ # Reduce word count by 50%
111
+ words = text.split()
112
+ text = " ".join(words[:len(words)//2])
113
+
114
+ if output_size == "1-3 word sentences":
115
+ text = reduce_to_sentences(text, 1, 3)
116
+ elif output_size == "2-5 word sentences":
117
+ text = reduce_to_sentences(text, 2, 5)
118
+ elif output_size == "3-7 word sentences":
119
+ text = reduce_to_sentences(text, 3, 7)
120
+ elif output_size == "5-9 word sentences":
121
+ text = reduce_to_sentences(text, 5, 9)
122
+ elif output_size == "6-11 word sentences":
123
+ text = reduce_to_sentences(text, 6, 11)
124
+
125
+ if bullet_points:
126
+ text = reduce_to_bullet_points(text, 1, 11)
127
+
128
+ if humanize_text:
129
+ text = humanize(text)
130
+
131
+ return text
132
+
133
+ def reduce_to_bullet_points(text, min_words, max_words):
134
+ sentences = text.split('.')
135
+ bullet_points = []
136
+ for sentence in sentences:
137
+ words = sentence.strip().split()
138
+ if min_words <= len(words) <= max_words:
139
+ bullet_points.append(f"- {' '.join(words)}")
140
+ return '\n'.join(bullet_points)
141
+
142
+ def reduce_to_sentences(text, min_words, max_words):
143
+ sentences = text.split('.')
144
+ filtered_sentences = []
145
+ for sentence in sentences:
146
+ words = sentence.strip().split()
147
+ if min_words <= len(words) <= max_words:
148
+ filtered_sentences.append(sentence.strip())
149
+ return ' '.join(filtered_sentences)
150
+
151
+ def humanize(text):
152
+ # This can be replaced with a more sophisticated humanization logic as needed
153
+ return text.replace(". ", ". Let's consider this further. ")
154
+
155
+ st.sidebar.info("build by dw")
156
+
157
+ if __name__ == "__main__":
158
+ main()