Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import re | |
| import json | |
| # Function to parse SQL file content | |
| def parse_sql_content(sql_lines): | |
| parsed_data = [] | |
| insert_regex = re.compile(r"INSERT INTO `.*` \(`id`,`question`,`answer`,`explanation`\) VALUES \((\d+),\'(.+?)\',\'(.+?)\',\'(.+?)\'\);") | |
| for line in sql_lines: | |
| match = insert_regex.match(line) | |
| if match: | |
| answer_json = match.group(3).replace("\\", "").replace("'", '"') | |
| try: | |
| answer_data = json.loads(answer_json) | |
| except json.JSONDecodeError: | |
| answer_data = answer_json | |
| data = { | |
| "id": int(match.group(1)), | |
| "question": match.group(2), | |
| "answer": answer_data, | |
| "explanation": match.group(4) | |
| } | |
| parsed_data.append(data) | |
| return parsed_data | |
| # Streamlit UI | |
| st.title("SQL Content Viewer") | |
| # File uploader | |
| uploaded_file = st.file_uploader("Upload your SQL file", type="sql") | |
| if uploaded_file is not None: | |
| sql_content = uploaded_file.getvalue().decode("utf-8").split("\n") | |
| # Parse SQL content | |
| parsed_data = parse_sql_content(sql_content) | |
| # Display content | |
| for entry in parsed_data: | |
| st.markdown(f"# ID: {entry['id']}") | |
| st.markdown("### Question") | |
| st.markdown(entry['question'], unsafe_allow_html=True) | |
| st.markdown("### Answer") | |
| try: | |
| st.write(json.dumps(entry['answer'], indent=4)) | |
| except: | |
| st.markdown(entry['answer'], unsafe_allow_html=True) | |
| st.markdown("### Explanation") | |
| st.markdown(entry['explanation'], unsafe_allow_html=True) # Display HTML content | |