ashwinR/CodeExplainer
0
1import streamlit as st2import json3from pathlib import Path4import requests5from gpt4free import you, theb6from streamlit_star_rating import st_star_rating7import requests8import time9from io import StringIO10 11 12st.text('')13st.title("Code Explainer")14st.text('')15 16source = st.radio("How would you like to start? Choose an option below",17 ("I want to input some text", "I want to upload a file"))18st.text('')19 20s_example = """21 class Solution(object):22 def isValid(self, s):23 stack = []24 mapping = {")": "(", "}": "{", "]": "["}25 for char in s:26 if char in mapping:27 top_element = stack.pop() if stack else '#'28 if mapping[char] != top_element:29 return False30 else:31 stack.append(char)32 return not stack33 """34 35if source == 'I want to input some text':36 input_su = st.text_area("Use the example below or input your own code with appropriate indentations)", value=s_example, max_chars=10000, height=330)37 if st.button('Explain Code'):38 with st.spinner('Processing...'):39 time.sleep(2)40 st.markdown('___')41 prompt=f"Explain this code. Lets think Step by step. : {input_su}"42 response = []43 for token in theb.Completion.create(prompt):44 response.append(print(token, end='', flush=True))45 st.write('Results!')46 st.write("".join(response))47 stars = st_star_rating("How satisfied are you with the response?", 5, 0, 40)48 st.balloons()49if source == 'I want to upload a file':50 file = st.file_uploader('Upload your file here',type=['txt'])51 if file is not None:52 with st.spinner('Converting your code to explanations...'):53 time.sleep(2)54 stringio = StringIO(file.getvalue().decode("utf-8"))55 string_data = stringio.read()56 time.sleep(2)57 st.markdown('___')58 response = you.Completion.create(59 prompt=f"Explain this code. Lets think Step by step : {string_data}"60 )61 62 st.write(response.text)63 stars = st_star_rating("How satisfied are you with the response?", 5, 0, 40)64 st.caption("")65 st.balloons()