Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import glob
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from reportlab.pdfgen import canvas
|
| 12 |
+
from reportlab.lib.utils import ImageReader
|
| 13 |
+
import mistune
|
| 14 |
+
from gtts import gTTS
|
| 15 |
+
|
| 16 |
+
# Page setup
|
| 17 |
+
st.set_page_config(page_title="PDF & Code Interpreter", layout="wide", page_icon="๐")
|
| 18 |
+
|
| 19 |
+
# Tabs
|
| 20 |
+
tab1, tab2 = st.tabs(["๐ PDF Composer","๐งช CodeInterpreter"])
|
| 21 |
+
|
| 22 |
+
# --- Tab 1: PDF Composer ---
|
| 23 |
+
with tab1:
|
| 24 |
+
st.title("๐ผ๏ธ๐ PDF Composer & Voice Generator")
|
| 25 |
+
# Upload Markdown
|
| 26 |
+
md_file = st.file_uploader("Upload Markdown (.md)", type=["md"] )
|
| 27 |
+
md_text = ""
|
| 28 |
+
if md_file:
|
| 29 |
+
md_text = md_file.getvalue().decode("utf-8")
|
| 30 |
+
else:
|
| 31 |
+
md_text = st.text_area("Or enter markdown text", height=200)
|
| 32 |
+
# Convert markdown to plain text
|
| 33 |
+
plain = mistune.HTMLRenderer().render(mistune.create_markdown()) if not md_text.strip() else mistune.markdown(md_text)
|
| 34 |
+
|
| 35 |
+
# Generate audio
|
| 36 |
+
if st.button("๐ Generate Voice Track MP3"):
|
| 37 |
+
tts = gTTS(text=re.sub(r'<[^>]+>','', plain), lang='en')
|
| 38 |
+
fname = f"voice_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3"
|
| 39 |
+
tts.save(fname)
|
| 40 |
+
st.audio(fname)
|
| 41 |
+
|
| 42 |
+
# Upload Images
|
| 43 |
+
imgs = st.file_uploader("Upload Images", type=["png","jpg","jpeg"], accept_multiple_files=True)
|
| 44 |
+
# Reorder images
|
| 45 |
+
if imgs:
|
| 46 |
+
df = pd.DataFrame([{"name":f.name,"order":i} for i,f in enumerate(imgs)])
|
| 47 |
+
edited = st.experimental_data_editor(df, num_rows="fixed")
|
| 48 |
+
ordered = [imgs[df.loc[i,'name'] == f.name][0] for i in range(len(df))]
|
| 49 |
+
else:
|
| 50 |
+
ordered = []
|
| 51 |
+
|
| 52 |
+
# Generate PDF
|
| 53 |
+
if st.button("๐๏ธ Generate PDF"):
|
| 54 |
+
buf = io.BytesIO()
|
| 55 |
+
c = canvas.Canvas(buf)
|
| 56 |
+
# Add markdown text
|
| 57 |
+
y = 800
|
| 58 |
+
for line in re.sub(r'<[^>]+>','', plain).split('\n'):
|
| 59 |
+
c.drawString(40,y,line)
|
| 60 |
+
y -= 15
|
| 61 |
+
if y<50:
|
| 62 |
+
c.showPage(); y=800
|
| 63 |
+
# Add images
|
| 64 |
+
for imgf in ordered:
|
| 65 |
+
img = Image.open(imgf)
|
| 66 |
+
w,h = img.size
|
| 67 |
+
c.showPage()
|
| 68 |
+
c.drawImage(ImageReader(img),0,0,w,h)
|
| 69 |
+
c.save(); buf.seek(0)
|
| 70 |
+
pname = f"document_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 71 |
+
st.download_button("โฌ๏ธ Download PDF", data=buf, file_name=pname, mime="application/pdf")
|
| 72 |
+
|
| 73 |
+
# Show existing PDF files
|
| 74 |
+
st.markdown("### Available PDFs")
|
| 75 |
+
pdfs = sorted(glob.glob("*.pdf"))
|
| 76 |
+
for p in pdfs:
|
| 77 |
+
st.write(p, st.button("Download", key=p, args=(), kwargs={}, help="Download PDF"))
|
| 78 |
+
|
| 79 |
+
# --- Tab 2: CodeInterpreter ---
|
| 80 |
+
with tab2:
|
| 81 |
+
import io, sys
|
| 82 |
+
from contextlib import redirect_stdout
|
| 83 |
+
DEFAULT_CODE = '''import streamlit as st
|
| 84 |
+
import random
|
| 85 |
+
|
| 86 |
+
st.title("๐งช Test Program")
|
| 87 |
+
st.markdown("Welcome to this simple test application!")
|
| 88 |
+
|
| 89 |
+
col1, col2 = st.columns(2)
|
| 90 |
+
with col1:
|
| 91 |
+
number = st.number_input("Enter a number:",1,100,50)
|
| 92 |
+
mult = st.slider("Select multiplier:",1,10,5)
|
| 93 |
+
if st.button("Calculate"):
|
| 94 |
+
st.success(f"Result: {number*mult}")
|
| 95 |
+
with col2:
|
| 96 |
+
color = st.color_picker("Pick a color","#00f900")
|
| 97 |
+
st.markdown(f'<div style="background-color:{color};padding:20px;">Color {color}</div>',unsafe_allow_html=True)
|
| 98 |
+
if st.button("Random Number"):
|
| 99 |
+
st.balloons(); st.write(random.randint(1,100))
|
| 100 |
+
'''
|
| 101 |
+
def extract_python_code(md): return re.findall(r"```python\s*(.*?)```",md,re.DOTALL)
|
| 102 |
+
def execute_code(code):
|
| 103 |
+
buf = io.StringIO(); lv={}
|
| 104 |
+
try:
|
| 105 |
+
with redirect_stdout(buf): exec(code,{},lv)
|
| 106 |
+
return buf.getvalue(),None
|
| 107 |
+
except Exception as e: return None,str(e)
|
| 108 |
+
st.title("๐ Python Code Executor")
|
| 109 |
+
up = st.file_uploader("Upload .py or .md",type=['py','md'])
|
| 110 |
+
code = DEFAULT_CODE if 'code' not in st.session_state else st.session_state.code
|
| 111 |
+
if up:
|
| 112 |
+
txt=up.getvalue().decode();
|
| 113 |
+
if up.type=='text/markdown':
|
| 114 |
+
blocks=extract_python_code(txt)
|
| 115 |
+
code=blocks[0] if blocks else ''
|
| 116 |
+
else: code=txt
|
| 117 |
+
st.code(code,language='python')
|
| 118 |
+
else:
|
| 119 |
+
code=st.text_area("Code:",value=code,height=300)
|
| 120 |
+
st.session_state.code=code
|
| 121 |
+
if st.button("โถ๏ธ Run Code"):
|
| 122 |
+
out,err=execute_code(code)
|
| 123 |
+
if err: st.error(err)
|
| 124 |
+
elif out: st.code(out)
|
| 125 |
+
else: st.success("Executed with no output.")
|
| 126 |
+
if st.button("๐๏ธ Clear Code"): st.session_state.code=''; st.rerun()
|