Francesco-A commited on
Commit
4e66fbc
·
1 Parent(s): 111993c

app_upload

Browse files
Files changed (4) hide show
  1. .gitignore +160 -0
  2. ZeroShotDataAug.pdf +0 -0
  3. app.py +121 -0
  4. requirements.txt +16 -0
.gitignore ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
ZeroShotDataAug.pdf ADDED
Binary file (472 kB). View file
 
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Langchain_apps/langchain_summarization_app.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['hub_llm', 'title', 'description', 'combine_prompt_template', 'pdf_example_1', 'pdf_example_2', 'prompt_example_1',
5
+ 'prompt_example_2', 'upload_file_input', 'custom_prompt_input', 'custom_chunk_input', 'chunk_size_input',
6
+ 'chunk_overlap_input', 'examples', 'outputs', 'iface', 'summarize']
7
+
8
+ # %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Langchain_apps/langchain_summarization_app.ipynb 3
9
+ from langchain import HuggingFaceHub, PromptTemplate, LLMChain
10
+ from langchain.text_splitter import CharacterTextSplitter
11
+ from langchain.chains.mapreduce import MapReduceChain
12
+ from langchain.prompts import PromptTemplate
13
+ from langchain.docstore.document import Document
14
+ from langchain.chains.summarize import load_summarize_chain
15
+ from langchain.document_loaders import PyPDFLoader
16
+
17
+ # %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Langchain_apps/langchain_summarization_app.ipynb 5
18
+ hub_llm = HuggingFaceHub(
19
+ repo_id="facebook/bart-large-cnn", # facebook/bart-large-cnn or "google/flan-t5-base" or "google/pegasus-xsum"
20
+ model_kwargs={
21
+ "temperature": 0.01, # Controls randomness (0.0: deterministic, 1.0: very random)
22
+ "max_new_tokens": 256*2, # Maximum number of tokens to generate in the summary
23
+ "min_length": 30, # Minimum length of the generated summary
24
+ "repetition_penalty": 1.2, # Penalizes repeated tokens (higher value = less repetition)
25
+ "top_k": 50, # Consider only the top k most likely tokens when generating
26
+ "top_p": 0.95, # Consider tokens with cumulative probability up to top_p
27
+ "early_stopping": True, # Stops generation when a certain condition is met (e.g., end-of-sequence token)
28
+ }
29
+ )
30
+
31
+ # %% ../drive/MyDrive/Codici/Python/Apps/Gradio_App/Langchain_apps/langchain_summarization_app.ipynb 15
32
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
33
+ import gradio as gr
34
+ import time
35
+
36
+ title="PDF Summarizer"
37
+ description="Summarize your PDF using a custom combine prompt."
38
+
39
+ # Default combine_prompt
40
+ combine_prompt_template = """Write a comprehensive summary of this academic article.
41
+
42
+ Divide the summary in:
43
+ 1. Main Objective of the paper
44
+ 2. Results
45
+
46
+ {text}
47
+
48
+ SUMMARY:"""
49
+
50
+ # Example PDF files and prompts
51
+ pdf_example_1 = './ZeroShotDataAug.pdf'
52
+ pdf_example_2 = './bert.pdf'
53
+ prompt_example_1 = """Write a comprehensive summary of this academic article.
54
+
55
+ Divide the summary in:
56
+ 1. Main Objective of the paper
57
+ 2. Results
58
+
59
+ {text}
60
+
61
+ SUMMARY:"""
62
+ prompt_example_2 = """Summarize the following document focusing on the key findings and methodology.
63
+
64
+ {text}
65
+
66
+ Summary:"""
67
+
68
+ # Implementation
69
+ def summarize(pdf_file, custom_prompt, custom_chunk, chunk_size, chunk_overlap):
70
+ try:
71
+ loader = PyPDFLoader(pdf_file.name)
72
+ if custom_chunk:
73
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
74
+ docs = loader.load_and_split(text_splitter=text_splitter)
75
+ else:
76
+ docs = loader.load_and_split()
77
+
78
+ PROMPT = PromptTemplate(template=custom_prompt, input_variables=['text'])
79
+ chain = load_summarize_chain(hub_llm, chain_type='map_reduce', combine_prompt=PROMPT)
80
+
81
+ # Introduce a delay before calling the API
82
+ time.sleep(1)
83
+ summary = chain.run(docs)
84
+ return summary
85
+ except Exception as e:
86
+ return f"An error occurred: {e}"
87
+
88
+ upload_file_input = gr.File(label="Upload PDF", type="filepath")
89
+ custom_prompt_input = gr.Textbox(label="Custom Prompt",
90
+ lines=10,
91
+ value=combine_prompt_template,
92
+ info="Define your own prompt or leave empty for default.")
93
+ custom_chunk_input = gr.Checkbox(label="Custom Chunk", value=False, info="Recommended to be left unchecked")
94
+ chunk_size_input = gr.Number(label="Chunk Size", value=700,minimum=500,maximum=1000,step=100)
95
+ chunk_overlap_input = gr.Number(label="Chunk Overlap", value=50,minimum=10,maximum=100,step=100)
96
+
97
+ examples=[
98
+ [pdf_example_1, prompt_example_1, False, 700, 50],
99
+ # [pdf_example_2, prompt_example_2, False, 700, 50]
100
+ ]
101
+
102
+ outputs = gr.Textbox(label="Summary")
103
+
104
+ iface = gr.Interface(
105
+ title=title,
106
+ description=description,
107
+ fn=summarize,
108
+ inputs=[upload_file_input,
109
+ custom_prompt_input,
110
+ custom_chunk_input,
111
+ chunk_size_input,
112
+ chunk_overlap_input
113
+ ],
114
+ outputs=outputs,
115
+ examples=examples,
116
+ )
117
+
118
+ iface.launch(
119
+ debug=False,
120
+ # share=False,
121
+ )
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio==5.8.0
2
+ gradio_client==1.5.1
3
+ geopandas==1.0.1
4
+ pandas==2.2.2
5
+ pandas-datareader==0.10.0
6
+ pandas-gbq==0.24.0
7
+ pandas-stubs==2.2.2.240909
8
+ sklearn-pandas==2.2.0
9
+ sentence-transformers==3.2.1
10
+ transformers==4.46.3
11
+ sentence-transformers==3.2.1
12
+ langchain==0.3.10
13
+ langchain-community==0.3.10
14
+ langchain-core==0.3.22
15
+ langchain-text-splitters==0.3.2
16
+ pypdf==5.1.0