Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import openai
|
| 3 |
import urllib.parse
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Function to call OpenAI GPT model for prompt processing
|
| 6 |
def get_diagram_code(prompt, diagram_type, api_key):
|
|
@@ -24,6 +26,18 @@ def get_diagram_code(prompt, diagram_type, api_key):
|
|
| 24 |
st.error(f"Error: {e}")
|
| 25 |
return None
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Streamlit App UI
|
| 28 |
def main():
|
| 29 |
st.title("Generate Diagrams using GPT-4 and MermaidFlow")
|
|
@@ -60,12 +74,33 @@ def main():
|
|
| 60 |
st.markdown(f'<iframe src="{mermaidflow_url}" width="100%" height="600px"></iframe>', unsafe_allow_html=True)
|
| 61 |
|
| 62 |
# Provide a direct link to MermaidFlow editor for convenience
|
| 63 |
-
st.markdown(f"Copy and paste the Generated Mermaid Code in the Live Editor and download the generated diagram by clicking on Export as SVG format to get
|
| 64 |
|
| 65 |
else:
|
| 66 |
st.error("Failed to generate diagram code.")
|
| 67 |
else:
|
| 68 |
st.error("Please enter a prompt.")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if __name__ == "__main__":
|
| 71 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import openai
|
| 3 |
import urllib.parse
|
| 4 |
+
import cairosvg
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
# Function to call OpenAI GPT model for prompt processing
|
| 8 |
def get_diagram_code(prompt, diagram_type, api_key):
|
|
|
|
| 26 |
st.error(f"Error: {e}")
|
| 27 |
return None
|
| 28 |
|
| 29 |
+
# Function to convert SVG to PNG
|
| 30 |
+
def convert_svg_to_png(svg_file):
|
| 31 |
+
try:
|
| 32 |
+
# Read the uploaded SVG file and convert it to PNG using cairosvg
|
| 33 |
+
png_output = io.BytesIO()
|
| 34 |
+
cairosvg.svg2png(file_obj=svg_file, write_to=png_output)
|
| 35 |
+
png_output.seek(0)
|
| 36 |
+
return png_output
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"Error during SVG to PNG conversion: {e}")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
# Streamlit App UI
|
| 42 |
def main():
|
| 43 |
st.title("Generate Diagrams using GPT-4 and MermaidFlow")
|
|
|
|
| 74 |
st.markdown(f'<iframe src="{mermaidflow_url}" width="100%" height="600px"></iframe>', unsafe_allow_html=True)
|
| 75 |
|
| 76 |
# Provide a direct link to MermaidFlow editor for convenience
|
| 77 |
+
st.markdown(f"Copy and paste the Generated Mermaid Code in the Live Editor and download the generated diagram by clicking on Export as SVG format to get a high-resolution generated image.")
|
| 78 |
|
| 79 |
else:
|
| 80 |
st.error("Failed to generate diagram code.")
|
| 81 |
else:
|
| 82 |
st.error("Please enter a prompt.")
|
| 83 |
|
| 84 |
+
# Add SVG file upload and conversion section
|
| 85 |
+
st.subheader("SVG to PNG Converter")
|
| 86 |
+
svg_file = st.file_uploader("Upload an SVG file to convert to PNG", type="svg")
|
| 87 |
+
|
| 88 |
+
if svg_file:
|
| 89 |
+
st.write("SVG file uploaded successfully!")
|
| 90 |
+
png_file = convert_svg_to_png(svg_file)
|
| 91 |
+
|
| 92 |
+
if png_file:
|
| 93 |
+
st.image(png_file, caption="Converted PNG Image", use_column_width=True)
|
| 94 |
+
|
| 95 |
+
# Provide a download link for the PNG file
|
| 96 |
+
st.download_button(
|
| 97 |
+
label="Download PNG",
|
| 98 |
+
data=png_file,
|
| 99 |
+
file_name="converted_image.png",
|
| 100 |
+
mime="image/png"
|
| 101 |
+
)
|
| 102 |
+
else:
|
| 103 |
+
st.error("Error in converting the SVG file to PNG.")
|
| 104 |
+
|
| 105 |
if __name__ == "__main__":
|
| 106 |
main()
|