Mr-Vicky-01 commited on
Commit
e229672
·
verified ·
1 Parent(s): 90f42e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -105
app.py CHANGED
@@ -1,106 +1,104 @@
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:pachaiappan1102@gamil.com)")
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
 
 
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