File size: 12,881 Bytes
b7af658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43d73b0
b7af658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43d73b0
b7af658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from langchain.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import PDFMinerLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain import HuggingFaceHub
from langchain.chains.summarize import load_summarize_chain
from langchain.chains.llm_summarization_checker.base import LLMSummarizationCheckerChain
from langchain.prompts import PromptTemplate
import os
import gradio as gr
import shutil
import re
import tempfile
from pathlib import Path

api_token=os.environ['api']
os.environ["HUGGINFACEHUB_API_TOKEN"]=api_token

# api=userdata.get('api')
# api_token=api
# # api_token =
# os.environ["HUGGINFACEHUB_API_TOKEN"]=api_token

temp_dir = "/content/sample_data"

# file_path_dummy = "/content/2401.10231.pdf"
# if file_path_dummy.lower().endswith(".pdf") :
#     loader = TextLoader(file_path_dummy)
#     document= loader.load()
# print(document)

def data_ingestion(file_path):
    if not os.path.exists(file_path):
      raise ValueError(f"File path {file_path} does not exist.")

    path = Path(file_path)
    file_ext = path.suffix

    # file_ext = os.path.splitext(file_path)[-1]
    # if file_ext == ".pdf":

    if file_path.lower().endswith(".pdf"):
        loader = PDFMinerLoader(file_path)

    elif file_path.lower().endswith(".txt"):
        loader = TextLoader(file_path)

    else:
        loader = Docx2txtLoader(file_path)


    # document= loader.load()

    # loader = PDFMinerLoader(file_path)
    document= loader.load()

    length = len(document[0].page_content)

    # Replace CharacterTextSplitter with RecursiveCharacterTextSplitter
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=0.03*length, chunk_overlap=0)
    split_docs = text_splitter.split_documents(document)

    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': 'cpu'})

    llm = HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
                        model_kwargs={"temperature":1, "max_length":10000},
                        huggingfacehub_api_token=api_token)

    return split_docs

# text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
#     chunk_size=2000, chunk_overlap=0
# )
# split_docs = text_splitter.split_documents(document)

# documents=split_text_into_batches(str(document),400)
# len(documents)
# documents[0]
# #
# from langchain.text_splitter import CharacterTextSplitter
# text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=0)
# documents = text_splitter.split_documents(document)
# Embeddings

# from langchain.chains.question_answering import load_qa_chain

########## CHAIN 1 norm text

def chain1():
    prompt_template = """Please provide a summary of the given study material. Summarize the key concepts, findings, and important details. 
    Pay special attention to any definitions, theories, or conclusions presented in the text. 
    Your summary should be concise yet comprehensive, capturing the main points of the study material.
    Your job is to write a summary of the document such that every summary of the text is of 2 sentences.
    here is the content of the section:
    "{text}"

    SUMMARY:"""
    prompt = PromptTemplate.from_template(prompt_template)

    refine_template = (
        "Your job is to produce a final summary\n"
        # "We have provided an existing summary up to a certain point: {existing_answer}\n"
        "We have the opportunity to refine the existing summary"
        "(only if needed) with some more context below.\n"
        "------------\n"
        "{text}\n"
        "------------\n"
        "Given the new context, refine the original summary in English"
        "If the context isn't useful, return the original summary." )

    refine_prompt = PromptTemplate.from_template(refine_template)
    chain1 = load_summarize_chain(
        llm=HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
                        model_kwargs={"temperature":1, "max_length":10000},
                        huggingfacehub_api_token=api_token),
        chain_type="refine",
        question_prompt=prompt,
        # refine_prompt=refine_prompt,
        return_intermediate_steps=False,
        input_key="input_documents",
        output_key="output_text",
    )
    return chain1

# result = chain({"input_documents":split_docs}, return_only_outputs=True)

########## CHAIN 2 Legal Document

def chain2():
    prompt_template = """Summarize the provided legal document. Highlight key legal arguments, decisions, and any crucial precedents mentioned. 
    Include a concise overview of the case or legal matter, focusing on the most significant details. 
    Ensure the summary captures the essential legal aspects and implications of the document.
    Your job is to write a summary of the document such that every summary of the text is of 2 sentences.
    here is the content of the section:
    "{text}"

    SUMMARY:"""
    prompt = PromptTemplate.from_template(prompt_template)

    refine_template = (
        "Your job is to produce a final summary\n"
        # "We have provided an existing summary up to a certain point: {existing_answer}\n"
        "We have the opportunity to refine the existing summary"
        "(only if needed) with some more context below.\n"
        "------------\n"
        "{text}\n"
        "------------\n"
        "Given the new context, refine the original summary in English"
        "If the context isn't useful, return the original summary." )

    refine_prompt = PromptTemplate.from_template(refine_template)
    chain2 = load_summarize_chain(
        llm = HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
                        model_kwargs={"temperature":1, "max_length":10000},
                        huggingfacehub_api_token=api_token),
        chain_type = "refine",
        question_prompt = prompt,
        # refine_prompt = refine_prompt,
        return_intermediate_steps=False,
        input_key="input_documents",
        output_key="output_text",
    )
    return chain2

# result = chain({"input_documents":split_docs}, return_only_outputs=True)

########## CHAIN 3 arxiv_paper_1

def chain3():
    prompt_template = """You are being given a markdown document with headers, this is part of a larger arxiv paper. 
    Provide a summary of the given research paper. Capture the main research question, methodology, key findings, and any novel contributions made by the paper.
    Emphasize the significance of the research and its potential implications. 
    The summary should be succinct while conveying the essential information presented in the research paper.
    Your job is to write a summary of the document such that every summary of the text is of 2 sentences.
    here is the content of the section:
    "{text}"

    SUMMARY:"""
    prompt = PromptTemplate.from_template(prompt_template)

    refine_template = ("""You are presented with a collection of text snippets. Each snippet is a summary of a specific section from an academic paper published on arXiv. Your objective is to synthesize these snippets into a coherent, concise summary of the entire paper.

        DOCUMENT SNIPPETS:
        "{text}"

        INSTRUCTIONS: Craft a concise summary below, capturing the essence of the paper based on the provided snippets.
        It is also important that you highlight the key contributions of the paper, and 3 key takeaways from the paper.
        Lastly you should provide a list of 5 questions that you would ask the author of the paper if you had the chance. Remove all the backslash n (\n)
        SUMMARY:
        """
        )

    refine_prompt = PromptTemplate.from_template(refine_template)
    chain3 = load_summarize_chain(
        llm=HuggingFaceHub(repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
                        model_kwargs={"temperature":1, "max_length":10000},
                        huggingfacehub_api_token=api_token),
        chain_type="refine",
        question_prompt=prompt,
        # refine_prompt=refine_prompt,
        return_intermediate_steps=False,
        input_key="input_documents",
        output_key="output_text",
    )
    return chain3
# result = chain({"input_documents":split_docs}, return_only_outputs=True)
# chain.run(document)
# print(result["output_text"])

def chain_function(checkbox_values):
    if "Research Paper" in checkbox_values:
        output = chain3()
    elif "Legal Document" in checkbox_values:
        output = chain2()
    elif "General Text" in checkbox_values:
        output = chain1()
    else:
        output = "Please select a document type to run."
    return output

def result(chain, split_docs):
    summaries = []
    for doc in split_docs:
        result = chain({"input_documents": [doc]})
        # result = chain({"input_documents": [doc]}, return_only_outputs=True)
        summaries.append(result["output_text"])
    text_concat = ""
    for i in summaries:
      text_concat += i
    # output = re.sub(r'\n',"  ","   ",text_concat)
    return text_concat

title = """<p style="font-family:Century Gothic; text-align:center; font-size: 100px">S  I  M  P  L  I  F  Y</p>"""

# description = r"""<p style="font-family: Century Gothic; text-align:center; font-size: 100px">S  I  M  P  L  I  F  Y</p>
# """

# article = r"""
# If PhotoMaker is helpful, please help to ⭐ the <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'>Github Repo</a>. Thanks!
# [![GitHub Stars](https://img.shields.io/github/stars/TencentARC/PhotoMaker?style=social)](https://github.com/TencentARC/PhotoMaker)
# ---
# πŸ“ **Citation**
# <br>
# If our work is useful for your research, please consider citing:
# ```bibtex
# @article{li2023photomaker,
#   title={PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding},
#   author={Li, Zhen and Cao, Mingdeng and Wang, Xintao and Qi, Zhongang and Cheng, Ming-Ming and Shan, Ying},
#   booktitle={arXiv preprint arxiv:2312.04461},
#   year={2023}
# }
# ```
# πŸ“‹ **License**
# <br>
# Apache-2.0 LICENSE. Please refer to the [LICENSE file](https://huggingface.co/TencentARC/PhotoMaker/blob/main/LICENSE) for details.
# πŸ“§ **Contact**
# <br>
# If you have any questions, please feel free to reach me out at <b>zhenli1031@gmail.com</b>.
# """

# tips = r"""
# ### Usage tips of PhotoMaker
# 1. Upload more photos of the person to be customized to **improve ID fidelty**. If the input is Asian face(s), maybe consider adding 'asian' before the class word, e.g., `asian woman img`
# 2. When stylizing, does the generated face look too realistic? Adjust the **Style strength** to 30-50, the larger the number, the less ID fidelty, but the stylization ability will be better.
# 3. If you want to generate realistic photos, you could try switching to our other gradio application [PhotoMaker](https://huggingface.co/spaces/TencentARC/PhotoMaker).
# 4. For **faster** speed, reduce the number of generated images and sampling steps. However, please note that reducing the sampling steps may compromise the ID fidelity.
# """

# def process_file(file_obj):
#     destination_path = "/content/sample_data"  # Replace with your desired path
#     shutil.copy(file_obj, destination_path)  # Save file to specified path
#     return os.path.join(destination_path, file_obj)
def process_file(list_file_obj):
    # list_file_path = [x.name for x in list_file_obj if x is not None]
    # file_content = file_obj.data
    # with tempfile.TemporaryFile() as temp_file:
    #     temp_file.write(file_content)
    #     temp_file_path = temp_file.name
    return list_file_obj[0].name

def inference(checkbox_values, uploaded_file):
    file_path = process_file(uploaded_file)
    split_docs = data_ingestion(file_path)
    chain = chain_function(checkbox_values)
    summary = result(chain, split_docs)
    return summary

with gr.Blocks(theme="monochrome") as demo:
    gr.Markdown(title)

    with gr.Row():
        with gr.Column():
            checkbox_values = gr.CheckboxGroup(["Research Paper", "Legal Document", "General Text"], label="Choose the document type")
            uploaded_file = gr.Files(height=100, file_count="multiple", file_types=["text", ".docx", "pdf"], interactive=True, label="Upload your File.")
            btn = gr.Button("Submit")  # Place the button outside the Row for vertical alignment
        with gr.Column():
            txt = gr.Textbox(
                show_label=False,
                # placeholder="Simplify."
            )


    btn.click(
        fn=inference,
        inputs=[checkbox_values, uploaded_file],
        outputs=[txt],
        queue=False
) 


if __name__ == "__main__":
    demo.queue()
    demo.launch(debug = True)