CodeExplainer / app.py
ashwinR's picture
Create app.py
1a40b48
raw
history blame
2.6 kB
import streamlit as st
import numpy as np
import pandas as pd
import json
import altair as alt
from pathlib import Path
import requests
from gpt4free import you
from streamlit_star_rating import st_star_rating
import requests
import time
from io import StringIO
st.text('')
st.title("Code Explainer")
st.text('')
source = st.radio("How would you like to start? Choose an option below",
("I want to input some text", "I want to upload a file"))
st.text('')
s_example = """
class Solution(object):
def isValid(self, s):
stack = []
mapping = {")": "(", "}": "{", "]": "["}
for char in s:
if char in mapping:
top_element = stack.pop() if stack else '#'
if mapping[char] != top_element:
return False
else:
stack.append(char)
return not stack
"""
if source == 'I want to input some text':
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)
if st.button('Explain Code'):
with st.spinner('Processing...'):
time.sleep(2)
st.markdown('___')
response = you.Completion.create(
prompt=f"Explain this code. Lets think Step by step. : {input_su}",
detailed=True,
include_links=True
)
st.write('Results!')
st.write(response.text)
stars = st_star_rating("How satisfied are you with the response?", 5, 0, 40)
st.balloons()
if source == 'I want to upload a file':
file = st.file_uploader('Upload your file here',type=['txt'])
if file is not None:
with st.spinner('Converting your code to explanations...'):
time.sleep(2)
stringio = StringIO(file.getvalue().decode("utf-8"))
string_data = stringio.read()
time.sleep(2)
st.markdown('___')
response = you.Completion.create(
prompt=f"Explain this code. Lets think Step by step : {string_data}",
detailed=True,
include_links=True
)
st.write(response.text)
stars = st_star_rating("How satisfied are you with the response?", 5, 0, 40)
st.caption("")
st.balloons()