Spaces:
Sleeping
Sleeping
Implemented response length constraints and improved summarization for concise and structured demo output.
Browse files
app.py
CHANGED
@@ -44,7 +44,7 @@ def generate_engineer_response(user_text, tokenizer, model):
|
|
44 |
outputs = model.generate(
|
45 |
inputs["input_ids"],
|
46 |
attention_mask=inputs["attention_mask"],
|
47 |
-
max_new_tokens=
|
48 |
temperature=0.7,
|
49 |
do_sample=True,
|
50 |
top_p=0.85,
|
@@ -52,7 +52,7 @@ def generate_engineer_response(user_text, tokenizer, model):
|
|
52 |
no_repeat_ngram_size=2,
|
53 |
pad_token_id=tokenizer.pad_token_id
|
54 |
)
|
55 |
-
explanation =
|
56 |
return tokenizer.decode(outputs[0], skip_special_tokens=True), explanation
|
57 |
|
58 |
def generate_analyst_response(user_text, engineer_output, tokenizer, model):
|
@@ -68,7 +68,7 @@ Based on this, provide an actionable data-driven approach or solution to complem
|
|
68 |
outputs = model.generate(
|
69 |
inputs["input_ids"],
|
70 |
attention_mask=inputs["attention_mask"],
|
71 |
-
max_new_tokens=
|
72 |
temperature=0.7,
|
73 |
do_sample=True,
|
74 |
top_p=0.85,
|
@@ -76,7 +76,7 @@ Based on this, provide an actionable data-driven approach or solution to complem
|
|
76 |
no_repeat_ngram_size=2,
|
77 |
pad_token_id=tokenizer.pad_token_id
|
78 |
)
|
79 |
-
explanation =
|
80 |
return tokenizer.decode(outputs[0], skip_special_tokens=True), explanation
|
81 |
|
82 |
def summarize_conversation(conversation):
|
@@ -85,7 +85,7 @@ def summarize_conversation(conversation):
|
|
85 |
"""
|
86 |
summary = "**Final Plan:**\n"
|
87 |
for speaker, text in conversation:
|
88 |
-
if speaker
|
89 |
summary += f"- **{speaker}:** {text}\n"
|
90 |
return summary
|
91 |
|
@@ -116,11 +116,9 @@ if st.button("Start/Continue Conversation"):
|
|
116 |
model=modelE
|
117 |
)
|
118 |
st.session_state.conversation.append(("Engineer", engineer_resp))
|
119 |
-
st.session_state.conversation.append(("Engineer Explanation", engineer_explanation))
|
120 |
|
121 |
# Display Engineer response immediately
|
122 |
st.markdown(f"**Engineer:** {engineer_resp}")
|
123 |
-
st.markdown(f"<i>{engineer_explanation}</i>", unsafe_allow_html=True)
|
124 |
|
125 |
# Analyst generates a response based on engineer's output
|
126 |
with st.spinner("Analyst is analyzing data and providing insights..."):
|
@@ -131,11 +129,9 @@ if st.button("Start/Continue Conversation"):
|
|
131 |
model=modelA
|
132 |
)
|
133 |
st.session_state.conversation.append(("Analyst", analyst_resp))
|
134 |
-
st.session_state.conversation.append(("Analyst Explanation", analyst_explanation))
|
135 |
|
136 |
# Display Analyst response immediately
|
137 |
st.markdown(f"**Analyst:** {analyst_resp}")
|
138 |
-
st.markdown(f"<i>{analyst_explanation}</i>", unsafe_allow_html=True)
|
139 |
|
140 |
# Limit the conversation to 3 exchanges between Engineer and Analyst
|
141 |
for _ in range(2):
|
@@ -146,11 +142,7 @@ if st.button("Start/Continue Conversation"):
|
|
146 |
model=modelE
|
147 |
)
|
148 |
st.session_state.conversation.append(("Engineer", engineer_resp))
|
149 |
-
st.session_state.conversation.append(("Engineer Explanation", engineer_explanation))
|
150 |
-
|
151 |
-
# Display Engineer response immediately
|
152 |
st.markdown(f"**Engineer:** {engineer_resp}")
|
153 |
-
st.markdown(f"<i>{engineer_explanation}</i>", unsafe_allow_html=True)
|
154 |
|
155 |
with st.spinner("Analyst is analyzing data and providing insights..."):
|
156 |
analyst_resp, analyst_explanation = generate_analyst_response(
|
@@ -160,23 +152,18 @@ if st.button("Start/Continue Conversation"):
|
|
160 |
model=modelA
|
161 |
)
|
162 |
st.session_state.conversation.append(("Analyst", analyst_resp))
|
163 |
-
st.session_state.conversation.append(("Analyst Explanation", analyst_explanation))
|
164 |
-
|
165 |
-
# Display Analyst response immediately
|
166 |
st.markdown(f"**Analyst:** {analyst_resp}")
|
167 |
-
st.markdown(f"<i>{analyst_explanation}</i>", unsafe_allow_html=True)
|
168 |
|
169 |
# Generate the summary after the conversation
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
173 |
|
174 |
for speaker, text in st.session_state.conversation:
|
175 |
if speaker == "User":
|
176 |
st.markdown(f"**{speaker}:** {text}")
|
177 |
elif speaker == "Summary":
|
178 |
st.markdown(f"**{speaker}:** {text}")
|
179 |
-
elif "Explanation" in speaker:
|
180 |
-
st.markdown(f"<i>{speaker}: {text}</i>", unsafe_allow_html=True)
|
181 |
else:
|
182 |
st.markdown(f"**{speaker}:** {text}")
|
|
|
44 |
outputs = model.generate(
|
45 |
inputs["input_ids"],
|
46 |
attention_mask=inputs["attention_mask"],
|
47 |
+
max_new_tokens=60, # Limit to 60 new tokens
|
48 |
temperature=0.7,
|
49 |
do_sample=True,
|
50 |
top_p=0.85,
|
|
|
52 |
no_repeat_ngram_size=2,
|
53 |
pad_token_id=tokenizer.pad_token_id
|
54 |
)
|
55 |
+
explanation = "Engineer response generated based on user input." # Keep explanations concise
|
56 |
return tokenizer.decode(outputs[0], skip_special_tokens=True), explanation
|
57 |
|
58 |
def generate_analyst_response(user_text, engineer_output, tokenizer, model):
|
|
|
68 |
outputs = model.generate(
|
69 |
inputs["input_ids"],
|
70 |
attention_mask=inputs["attention_mask"],
|
71 |
+
max_new_tokens=60, # Limit to 60 new tokens
|
72 |
temperature=0.7,
|
73 |
do_sample=True,
|
74 |
top_p=0.85,
|
|
|
76 |
no_repeat_ngram_size=2,
|
77 |
pad_token_id=tokenizer.pad_token_id
|
78 |
)
|
79 |
+
explanation = "Analyst response generated based on Engineer's output." # Keep explanations concise
|
80 |
return tokenizer.decode(outputs[0], skip_special_tokens=True), explanation
|
81 |
|
82 |
def summarize_conversation(conversation):
|
|
|
85 |
"""
|
86 |
summary = "**Final Plan:**\n"
|
87 |
for speaker, text in conversation:
|
88 |
+
if speaker not in ["User", "Summary"]:
|
89 |
summary += f"- **{speaker}:** {text}\n"
|
90 |
return summary
|
91 |
|
|
|
116 |
model=modelE
|
117 |
)
|
118 |
st.session_state.conversation.append(("Engineer", engineer_resp))
|
|
|
119 |
|
120 |
# Display Engineer response immediately
|
121 |
st.markdown(f"**Engineer:** {engineer_resp}")
|
|
|
122 |
|
123 |
# Analyst generates a response based on engineer's output
|
124 |
with st.spinner("Analyst is analyzing data and providing insights..."):
|
|
|
129 |
model=modelA
|
130 |
)
|
131 |
st.session_state.conversation.append(("Analyst", analyst_resp))
|
|
|
132 |
|
133 |
# Display Analyst response immediately
|
134 |
st.markdown(f"**Analyst:** {analyst_resp}")
|
|
|
135 |
|
136 |
# Limit the conversation to 3 exchanges between Engineer and Analyst
|
137 |
for _ in range(2):
|
|
|
142 |
model=modelE
|
143 |
)
|
144 |
st.session_state.conversation.append(("Engineer", engineer_resp))
|
|
|
|
|
|
|
145 |
st.markdown(f"**Engineer:** {engineer_resp}")
|
|
|
146 |
|
147 |
with st.spinner("Analyst is analyzing data and providing insights..."):
|
148 |
analyst_resp, analyst_explanation = generate_analyst_response(
|
|
|
152 |
model=modelA
|
153 |
)
|
154 |
st.session_state.conversation.append(("Analyst", analyst_resp))
|
|
|
|
|
|
|
155 |
st.markdown(f"**Analyst:** {analyst_resp}")
|
|
|
156 |
|
157 |
# Generate the summary after the conversation
|
158 |
+
with st.spinner("Generating the final plan..."):
|
159 |
+
final_plan = summarize_conversation(st.session_state.conversation)
|
160 |
+
st.session_state.conversation.append(("Summary", final_plan))
|
161 |
+
st.markdown(final_plan)
|
162 |
|
163 |
for speaker, text in st.session_state.conversation:
|
164 |
if speaker == "User":
|
165 |
st.markdown(f"**{speaker}:** {text}")
|
166 |
elif speaker == "Summary":
|
167 |
st.markdown(f"**{speaker}:** {text}")
|
|
|
|
|
168 |
else:
|
169 |
st.markdown(f"**{speaker}:** {text}")
|