Spaces:
Sleeping
Sleeping
Mr-Vicky-01
commited on
Commit
•
d3b93e6
1
Parent(s):
dfaf9a0
Upload 3 files
Browse files- app.py +55 -0
- file_creator.py +40 -0
- llm.py +19 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import llm
|
3 |
+
from file_creator import Create_Doc
|
4 |
+
|
5 |
+
model = llm.Model()
|
6 |
+
unvalid_image_text = 'please upload a valid screenshot.'
|
7 |
+
|
8 |
+
|
9 |
+
st.markdown("""
|
10 |
+
<style>
|
11 |
+
.justified-text {
|
12 |
+
text-align: justify;
|
13 |
+
}
|
14 |
+
</style>
|
15 |
+
""", unsafe_allow_html=True)
|
16 |
+
|
17 |
+
|
18 |
+
with st.sidebar:
|
19 |
+
st.header("ABOUT:")
|
20 |
+
|
21 |
+
st.caption("""
|
22 |
+
<div class="justified-text">
|
23 |
+
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.
|
24 |
+
</div>
|
25 |
+
""", unsafe_allow_html=True)
|
26 |
+
|
27 |
+
for _ in range(17):
|
28 |
+
st.write("")
|
29 |
+
st.subheader("Build By:")
|
30 |
+
st.write("[Pachaiappan❤️](https://mr-vicky-01.github.io/Portfolio)")
|
31 |
+
st.write("contact: [Email](mailto:[email protected])")
|
32 |
+
|
33 |
+
st.title("Document Creator")
|
34 |
+
st.text("Updload your Screenshot to convert into a word document.")
|
35 |
+
uploaded_file = st.file_uploader("", type=["png", "jpg", "jpeg"])
|
36 |
+
|
37 |
+
if uploaded_file:
|
38 |
+
st.image(uploaded_file)
|
39 |
+
|
40 |
+
button = st.button("Generate Document")
|
41 |
+
if button:
|
42 |
+
with st.spinner("Generating a Document"):
|
43 |
+
text = model.get_response(uploaded_file)
|
44 |
+
st.write(text)
|
45 |
+
|
46 |
+
if text.lower().strip() != unvalid_image_text:
|
47 |
+
doc = Create_Doc()
|
48 |
+
doc_buffer = doc.markdown_to_word(text)
|
49 |
+
st.download_button(
|
50 |
+
label="Download",
|
51 |
+
data=doc_buffer,
|
52 |
+
file_name="output.docx",
|
53 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
54 |
+
)
|
55 |
+
|
file_creator.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from docx import Document
|
2 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
3 |
+
from docx.shared import Pt
|
4 |
+
from io import BytesIO
|
5 |
+
import markdown
|
6 |
+
|
7 |
+
class Create_Doc:
|
8 |
+
def __init__(self) -> None:
|
9 |
+
self.doc = Document()
|
10 |
+
|
11 |
+
def markdown_to_word(self,markdown_text):
|
12 |
+
# Convert Markdown to HTML
|
13 |
+
# html = markdown.markdown(markdown_text)
|
14 |
+
|
15 |
+
# Parse the Markdown text and add formatted content to the document
|
16 |
+
for line in markdown_text.split('\n'):
|
17 |
+
if line.startswith('# '):
|
18 |
+
heading = line[2:]
|
19 |
+
p = self.doc.add_heading(heading, level=1)
|
20 |
+
elif line.startswith('## '):
|
21 |
+
heading = line[3:]
|
22 |
+
p = self.doc.add_heading(heading, level=2)
|
23 |
+
elif line.startswith('### '):
|
24 |
+
heading = line[4:]
|
25 |
+
p = self.doc.add_heading(heading, level=3)
|
26 |
+
elif line.startswith('- '):
|
27 |
+
item = line[2:]
|
28 |
+
p = self.doc.add_paragraph(item, style='ListBullet')
|
29 |
+
else:
|
30 |
+
p = self.doc.add_paragraph(line)
|
31 |
+
|
32 |
+
# Adjust paragraph formatting (optional)
|
33 |
+
p.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
|
34 |
+
p.style.font.size = Pt(12)
|
35 |
+
|
36 |
+
# Save the document to a BytesIO object
|
37 |
+
buffer = BytesIO()
|
38 |
+
self.doc.save(buffer)
|
39 |
+
buffer.seek(0)
|
40 |
+
return buffer
|
llm.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import google.generativeai as genai
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from PIL import Image
|
4 |
+
import os
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
|
8 |
+
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
|
9 |
+
|
10 |
+
class Model:
|
11 |
+
def __init__(self) -> None:
|
12 |
+
self.model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
13 |
+
|
14 |
+
def get_response(self, image):
|
15 |
+
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
|
16 |
+
important note: if the screenshot not contain any text means you must say 'please upload a valid screenshot.'"""
|
17 |
+
img = Image.open(image)
|
18 |
+
response = self.model.generate_content([prompt, img])
|
19 |
+
return response.text
|