Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import markdown
|
| 5 |
+
from docx import Document
|
| 6 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
| 7 |
+
from docx.shared import Pt
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
| 12 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
| 13 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 14 |
+
|
| 15 |
+
def response(image):
|
| 16 |
+
prompt = """You are an intelligent document creator. Could you please extract the words from the given screenshot and provide me document text that matches exact screenshot font and look
|
| 17 |
+
important note: if the screenshot not contain any text means you must say 'please upload a valid screenshot'"""
|
| 18 |
+
img = Image.open(image)
|
| 19 |
+
response = model.generate_content([prompt, img])
|
| 20 |
+
return response.text
|
| 21 |
+
|
| 22 |
+
def markdown_to_word(markdown_text):
|
| 23 |
+
# Create a new Word document
|
| 24 |
+
doc = Document()
|
| 25 |
+
|
| 26 |
+
for line in markdown_text.split('\n'):
|
| 27 |
+
if line.startswith('# '):
|
| 28 |
+
heading = line[2:]
|
| 29 |
+
p = doc.add_heading(heading, level=1)
|
| 30 |
+
elif line.startswith('## '):
|
| 31 |
+
heading = line[3:]
|
| 32 |
+
p = doc.add_heading(heading, level=2)
|
| 33 |
+
elif line.startswith('### '):
|
| 34 |
+
heading = line[4:]
|
| 35 |
+
p = doc.add_heading(heading, level=3)
|
| 36 |
+
elif line.startswith('- '):
|
| 37 |
+
item = line[2:]
|
| 38 |
+
p = doc.add_paragraph(item, style='ListBullet')
|
| 39 |
+
else:
|
| 40 |
+
p = doc.add_paragraph()
|
| 41 |
+
words = line.split(' ')
|
| 42 |
+
for word in words:
|
| 43 |
+
word = word.strip()
|
| 44 |
+
if word.startswith('**') and word.endswith('**'):
|
| 45 |
+
run = p.add_run(word[2:-2])
|
| 46 |
+
run.bold = True
|
| 47 |
+
elif word.startswith('*') and word.endswith('*'):
|
| 48 |
+
run = p.add_run(word[1:-1])
|
| 49 |
+
run.italic = True
|
| 50 |
+
else:
|
| 51 |
+
p.add_run(word)
|
| 52 |
+
p.add_run(' ')
|
| 53 |
+
|
| 54 |
+
# Save the document to a BytesIO object
|
| 55 |
+
buffer = BytesIO()
|
| 56 |
+
doc.save(buffer)
|
| 57 |
+
buffer.seek(0)
|
| 58 |
+
return buffer
|
| 59 |
+
|
| 60 |
+
st.title("SCREENSHOT🖼️ - DOCUMENT📃")
|
| 61 |
+
st.markdown("""
|
| 62 |
+
<style>
|
| 63 |
+
.justified-text {
|
| 64 |
+
text-align: justify;
|
| 65 |
+
}
|
| 66 |
+
</style>
|
| 67 |
+
""", unsafe_allow_html=True)
|
| 68 |
+
with st.sidebar:
|
| 69 |
+
st.header("ABOUT:")
|
| 70 |
+
|
| 71 |
+
st.caption("""
|
| 72 |
+
<div class="justified-text">
|
| 73 |
+
Screenshot to Document file Creator is an AI powered app that allows users to effortlessly convert their screenshots into Word documents. Simply upload a screenshot, and the app will generate a Word document based on the image provided, ensuring a seamless and efficient conversion process. Ideal for anyone looking to quickly turn visual content into editable text documents.
|
| 74 |
+
</div>
|
| 75 |
+
""", unsafe_allow_html=True)
|
| 76 |
+
|
| 77 |
+
for _ in range(17):
|
| 78 |
+
st.write("")
|
| 79 |
+
st.subheader("Build By:")
|
| 80 |
+
st.write("[Pachaiappan❤️](https://mr-vicky-01.github.io/Portfolio)")
|
| 81 |
+
st.write("contact: [Email](mailto:pachaiappan1102@gamil.com)")
|
| 82 |
+
|
| 83 |
+
fake_image_text = 'please upload a valid screenshot.'
|
| 84 |
+
st.text("Upload your screenshot to convert it into a Word document")
|
| 85 |
+
uploaded_file = st.file_uploader("", type=["png", "jpg", "jpeg"])
|
| 86 |
+
if uploaded_file:
|
| 87 |
+
st.image(uploaded_file)
|
| 88 |
+
button = st.button("Generate Document")
|
| 89 |
+
if button:
|
| 90 |
+
with st.spinner("Generating a Document..."):
|
| 91 |
+
text = response(uploaded_file)
|
| 92 |
+
st.write(text)
|
| 93 |
+
|
| 94 |
+
if text.lower().strip() != fake_image_text:
|
| 95 |
+
doc_buffer = markdown_to_word(text)
|
| 96 |
+
st.download_button(
|
| 97 |
+
label="Download",
|
| 98 |
+
data=doc_buffer,
|
| 99 |
+
file_name="output.docx",
|
| 100 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 101 |
+
)
|
| 102 |
+
|