Spaces:
Sleeping
Sleeping
Mr-Vicky-01
commited on
Commit
•
1eb3216
1
Parent(s):
4e7b4be
Upload 2 files
Browse files- app.py +106 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from PIL import Image
|
5 |
+
import markdown
|
6 |
+
from docx import Document
|
7 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
8 |
+
from docx.shared import Pt
|
9 |
+
from io import BytesIO
|
10 |
+
import os
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
14 |
+
|
15 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
16 |
+
|
17 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
18 |
+
|
19 |
+
def response(image):
|
20 |
+
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
|
21 |
+
important note: if the screenshot not contain any text means you must say 'please upload a valid screenshot'"""
|
22 |
+
img = Image.open(image)
|
23 |
+
response = model.generate_content([prompt, img])
|
24 |
+
return response.text
|
25 |
+
|
26 |
+
# Function to convert Markdown to Word document
|
27 |
+
def markdown_to_word(markdown_text):
|
28 |
+
# Create a new Word document
|
29 |
+
doc = Document()
|
30 |
+
|
31 |
+
for line in markdown_text.split('\n'):
|
32 |
+
if line.startswith('# '):
|
33 |
+
heading = line[2:]
|
34 |
+
p = doc.add_heading(heading, level=1)
|
35 |
+
elif line.startswith('## '):
|
36 |
+
heading = line[3:]
|
37 |
+
p = doc.add_heading(heading, level=2)
|
38 |
+
elif line.startswith('### '):
|
39 |
+
heading = line[4:]
|
40 |
+
p = doc.add_heading(heading, level=3)
|
41 |
+
elif line.startswith('- '):
|
42 |
+
item = line[2:]
|
43 |
+
p = doc.add_paragraph(item, style='ListBullet')
|
44 |
+
else:
|
45 |
+
p = doc.add_paragraph()
|
46 |
+
words = line.split(' ')
|
47 |
+
for word in words:
|
48 |
+
if word.startswith('**') and word.endswith('**'):
|
49 |
+
run = p.add_run(word[2:-2])
|
50 |
+
run.bold = True
|
51 |
+
elif word.startswith('*') and word.endswith('*'):
|
52 |
+
run = p.add_run(word[1:-1])
|
53 |
+
run.italic = True
|
54 |
+
else:
|
55 |
+
p.add_run(word)
|
56 |
+
p.add_run(' ')
|
57 |
+
|
58 |
+
# Save the document to a BytesIO object
|
59 |
+
buffer = BytesIO()
|
60 |
+
doc.save(buffer)
|
61 |
+
buffer.seek(0)
|
62 |
+
return buffer
|
63 |
+
|
64 |
+
st.title("Document Creator")
|
65 |
+
st.markdown("""
|
66 |
+
<style>
|
67 |
+
.justified-text {
|
68 |
+
text-align: justify;
|
69 |
+
}
|
70 |
+
</style>
|
71 |
+
""", unsafe_allow_html=True)
|
72 |
+
with st.sidebar:
|
73 |
+
st.header("ABOUT:")
|
74 |
+
|
75 |
+
st.caption("""
|
76 |
+
<div class="justified-text">
|
77 |
+
Document Creator is an innovative 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.
|
78 |
+
</div>
|
79 |
+
""", unsafe_allow_html=True)
|
80 |
+
|
81 |
+
for _ in range(17):
|
82 |
+
st.write("")
|
83 |
+
st.subheader("Build By:")
|
84 |
+
st.write("[Pachaiappan❤️](https://mr-vicky-01.github.io/Portfolio)")
|
85 |
+
st.write("contact: [Email](mailto:[email protected])")
|
86 |
+
|
87 |
+
fake_image_text = 'please upload a valid screenshot.'
|
88 |
+
|
89 |
+
uploaded_file = st.file_uploader("Updload your Screenshot", type=["png", "jpg", "jpeg"])
|
90 |
+
if uploaded_file:
|
91 |
+
st.image(uploaded_file)
|
92 |
+
button = st.button("Generate Document")
|
93 |
+
if button:
|
94 |
+
with st.spinner("Generating a Document"):
|
95 |
+
text = response(uploaded_file)
|
96 |
+
st.write(text)
|
97 |
+
|
98 |
+
if text.lower().strip() != fake_image_text:
|
99 |
+
doc_buffer = markdown_to_word(text)
|
100 |
+
st.download_button(
|
101 |
+
label="Download",
|
102 |
+
data=doc_buffer,
|
103 |
+
file_name="output.docx",
|
104 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
105 |
+
)
|
106 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
python-docx
|
5 |
+
markdown
|