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