drmurataltun commited on
Commit
cccd854
1 Parent(s): cc4a955

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +153 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ import google.generativeai as genai
4
+ import markdown
5
+ from docx import Document
6
+ from bs4 import BeautifulSoup
7
+ from PyPDF2 import PdfFileReader
8
+ import tempfile
9
+ import os
10
+
11
+ # Configure the API key
12
+ genai.configure(api_key=os.getenv('gemini_api'))
13
+
14
+ # Function to convert PDF to text
15
+ def pdf_to_text(file):
16
+ with open(file, 'rb') as f:
17
+ pdf = PdfFileReader(f)
18
+ text = ""
19
+ for page_num in range(pdf.numPages):
20
+ page = pdf.getPage(page_num)
21
+ text += page.extract_text()
22
+ return text
23
+
24
+ # Function to upload file to the Generative AI API
25
+ def upload_file(file_path):
26
+ st.write("Uploading file...")
27
+ text_file = genai.upload_file(path=file_path)
28
+ st.write(f"Completed upload: {text_file.uri}")
29
+ return text_file
30
+
31
+ # Function to convert text to Markdown
32
+ def to_markdown(text):
33
+ text = text.replace('•', ' *')
34
+ return textwrap.indent(text, '> ', predicate=lambda _: True)
35
+
36
+ chat_session = None
37
+
38
+ # Function to build the model
39
+ def build_model(text_file):
40
+ global chat_session
41
+ generation_config = {
42
+ "temperature": 0.2,
43
+ "top_p": 0.95,
44
+ "top_k": 64,
45
+ "max_output_tokens": 8192,
46
+ "response_mime_type": "text/plain",
47
+ }
48
+
49
+ model = genai.GenerativeModel(
50
+ model_name="gemini-1.5-flash",
51
+ generation_config=generation_config,
52
+ system_instruction="""Yüklenen belgedeki bilgilere göre Türkçe cevap ver.
53
+ Eğer sorunun cevabı belgede bulunmuyorsa 'Belgede Cevap Bulunmuyor' yaz.
54
+ """,
55
+ )
56
+
57
+ chat_session = model.start_chat(history=[])
58
+ response = chat_session.send_message(["Yüklenen belgeyi bir cümle ile özetle", text_file])
59
+
60
+ st.markdown(to_markdown(response.text))
61
+
62
+ # Function to interact with the chat model
63
+ def chat(prompt):
64
+ try:
65
+ response = chat_session.send_message(prompt)
66
+ markdown_text = to_markdown(response.text)
67
+ st.markdown(markdown_text)
68
+ return response.text
69
+ except ValueError:
70
+ st.write(response.prompt_feedback)
71
+ st.write(response.candidates[0].finish_reason)
72
+ st.write(response.candidates[0].safety_ratings)
73
+ except Exception as e:
74
+ st.write("An unexpected error occurred:", e)
75
+
76
+ # Function to generate a report based on questions
77
+ def generate_report(questions):
78
+ report_text = "\n## SORULARINIZ VE CEVAPLARI\n"
79
+ for question in questions:
80
+ report_text += f"\n## {question}\n"
81
+ answer = chat(question)
82
+ report_text += f"\n{answer}\n"
83
+ return report_text
84
+
85
+ # Function to convert Markdown to HTML
86
+ def convert_Markdown_to_HTML(report_text):
87
+ html_text = markdown.markdown(report_text)
88
+ return html_text
89
+
90
+ # Function to add HTML to a Word document
91
+ def add_html_to_word(html_text, doc):
92
+ soup = BeautifulSoup(html_text, 'html.parser')
93
+ for element in soup:
94
+ if element.name == 'h1':
95
+ doc.add_heading(element.get_text(), level=1)
96
+ elif element.name == 'h2':
97
+ doc.add_heading(element.get_text(), level=2)
98
+ elif element.name == 'h3':
99
+ doc.add_heading(element.get_text(), level=3)
100
+ elif element.name == 'h4':
101
+ doc.add_heading(element.get_text(), level=4)
102
+ elif element.name == 'h5':
103
+ doc.add_heading(element.get_text(), level=5)
104
+ elif element.name == 'h6':
105
+ doc.add_heading(element.get_text(), level=6)
106
+ elif element.name == 'p':
107
+ doc.add_paragraph(element.get_text())
108
+ elif element.name == 'ul':
109
+ for li in element.find_all('li'):
110
+ doc.add_paragraph(li.get_text(), style='List Bullet')
111
+ elif element.name == 'ol':
112
+ for li in element.find_all('li'):
113
+ doc.add_paragraph(li.get_text(), style='List Number')
114
+ elif element.name:
115
+ doc.add_paragraph(element.get_text())
116
+
117
+ # Streamlit interface
118
+ st.title("REPORT GENERATOR: ASK YOUR QUESTIONS TO A PDF FILE BY MURAT KARAKAYA AKADEMI")
119
+ st.write("Upload a PDF to ask questions and get the answers.")
120
+
121
+ uploaded_file = st.file_uploader("Upload PDF", type="pdf")
122
+ questions_input = st.text_area("Enter Questions", placeholder="Type your questions here, one per line.", height=150)
123
+
124
+ if uploaded_file and questions_input:
125
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
126
+ temp_file.write(uploaded_file.read())
127
+ temp_file_path = temp_file.name
128
+
129
+ text_content = pdf_to_text(temp_file_path)
130
+ text_file = upload_file(temp_file_path)
131
+ build_model(text_file)
132
+
133
+ questions = questions_input.split("\n")
134
+ report_text = generate_report(questions)
135
+
136
+ html_text = convert_Markdown_to_HTML(report_text)
137
+ doc = Document()
138
+ add_html_to_word(html_text, doc)
139
+
140
+ doc_name = os.path.basename(temp_file_path).replace(".pdf", ".docx")
141
+ doc_name = "Rapor " + doc_name
142
+ doc.save(doc_name)
143
+
144
+ st.markdown(report_text)
145
+ st.write("Document generated successfully!")
146
+
147
+ with open(doc_name, "rb") as file:
148
+ st.download_button(label="Download Report", data=file, file_name=doc_name)
149
+
150
+ os.remove(temp_file_path)
151
+ os.remove(doc_name)
152
+
153
+ genai.delete_file(text_file.name)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ markdown
4
+ python-docx
5
+ beautifulsoup4
6
+ PyPDF2