Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from parser import MinimalPromptParser | |
| def get_language_from_extension(extension): | |
| ext_map = { | |
| '.py': 'python', | |
| '.js': 'javascript', | |
| '.html': 'html', | |
| '.css': 'css', | |
| '.java': 'java', | |
| '.c': 'c', | |
| '.cpp': 'cpp', | |
| '.cs': 'csharp', | |
| '.rb': 'ruby', | |
| '.go': 'go', | |
| '.rs': 'rust', | |
| '.swift': 'swift', | |
| '.php': 'php', | |
| '.sql': 'sql' | |
| } | |
| return ext_map.get(extension, 'text') | |
| api_key = st.text_input("Enter your OpenAI API Key:", type="password") | |
| def llmcall(input): | |
| if not api_key: | |
| st.toast("Please provide your OpenAI key") | |
| return 'Shame' | |
| simple_review = MinimalPromptParser().parse_file('simplereview.md', {'code':input}) | |
| from openai import OpenAI | |
| client = OpenAI(api_key = api_key) | |
| completion = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=simple_review, | |
| temperature=0.1 | |
| ) | |
| return completion.choices[0].message.content | |
| # Title of the app | |
| st.title('Does my code suck?') | |
| st.subheader("FYI we do not store any of your code. Obviously. Why would we ? It sucks.") | |
| # Upload file input | |
| upload, text, repo = st.tabs(["File", "Copy-Paste", "Public Repo Link"]) | |
| with upload: | |
| uploaded_file = st.file_uploader("Choose a file") | |
| with repo: | |
| st.text_input("Your obnoxious repo link") | |
| if uploaded_file is not None: | |
| # To read file as string: | |
| extension = uploaded_file.name.split('.')[-1] | |
| file_extension = f".{extension}" | |
| language = get_language_from_extension(file_extension) | |
| st.header(f'This is a **** {language} file') | |
| # To read file as string: | |
| file_content = uploaded_file.read().decode("utf-8") | |
| with st.expander("See code"): | |
| # Get programming language | |
| user_input = st.text_area('Your not so good code:', value = file_content, height=500, disabled=True) | |
| else: | |
| with text: | |
| user_input = st.text_area('Or paste here your not so good code:', height=500) | |
| # Button to review the input | |
| if st.button('Review'): | |
| st.divider() | |
| st.header("Uff, there you go") | |
| # Output displayed when the button is clicked | |
| with st.spinner('Reading...'): | |
| reviewed_coded = llmcall(user_input) | |
| markdown, raw = st.tabs(["Markdown", "Raw"]) | |
| with markdown: | |
| st.download_button('Download report', data=reviewed_coded, file_name='report.txt') | |
| st.markdown(reviewed_coded) | |
| with raw: | |
| st.code(reviewed_coded) | |