Skip to content

Commit 113105e

Browse files
committed
Improvements on output copy & paste
1 parent a8e298a commit 113105e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

app.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def process_user_code(user_code, conversation, programming_language, style_guide
7676
"""
7777
Process the user's code and generate a response using the conversation chain.
7878
"""
79-
prompt = f"Please rewrite the provided {programming_language} code to adhere strictly to the {style_guide} standards. Ensure the output consists solely of the revised code, ready for copy-paste:\n\n{user_code}. Please ensure to that the output only consists of the revised code this is very important!"
79+
prompt = f"Please rewrite the provided {programming_language} code to adhere strictly to the {style_guide} standards. Ensure the output consists solely of the revised code, enclosed within triple backticks for easy copy-paste, without specifying the programming language on the first line:\n\n{user_code}. Please ensure that the output only consists of the revised code within triple backticks, as this is very important!"
8080
response = conversation(prompt)
8181
message = {'human': prompt, 'AI': response['response']}
8282
st.session_state.chat_history.append(message)
@@ -88,7 +88,7 @@ def main():
8888
"""
8989
groq_api_key = os.environ['GROQ_API_KEY']
9090
initialize_session_state()
91-
st.title("Lightning ⚡️ Code Style Guide Assistant")
91+
st.title("Lightning ⚡️ Code Style Guide Assistant & Code Translator")
9292
st.markdown("Get your code rewritten according to popular style guides by Lightning, an ultra-fast AI chatbot powered by Groq LPUs!!")
9393

9494
model, programming_language, style_guide = display_customization_options()
@@ -121,7 +121,22 @@ def main():
121121

122122
with st.expander("Rewritten Code"):
123123
response = conversation(user_code)
124-
st.code(response['response'], language=programming_language.lower())
124+
ai_response = response['response']
125+
126+
# Extract the code within triple backticks
127+
code_start = ai_response.find('```') + 3
128+
code_end = ai_response.find('```', code_start)
129+
code = ai_response[code_start:code_end]
130+
131+
# Extract the text before and after the code
132+
text_before_code = ai_response[:code_start-3].strip()
133+
text_after_code = ai_response[code_end+3:].strip()
134+
135+
# Display the text and code separately
136+
st.write(text_before_code)
137+
st.code(code, language=programming_language.lower())
138+
st.write(text_after_code)
139+
125140
st.session_state.chat_history[-1]["AI"] = response['response']
126141

127142

0 commit comments

Comments
 (0)