arcan3 commited on
Commit
7401fcc
·
1 Parent(s): 0ad49e7

huggingface adaptation of app

Browse files
Files changed (3) hide show
  1. .gitignore +161 -0
  2. app.py +143 -0
  3. requirements.txt +63 -0
.gitignore ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ *.ipynb
124
+ .env
125
+ .venv
126
+ env/
127
+ venv/
128
+ ENV/
129
+ env.bak/
130
+ venv.bak/
131
+
132
+ # Spyder project settings
133
+ .spyderproject
134
+ .spyproject
135
+
136
+ # Rope project settings
137
+ .ropeproject
138
+
139
+ # mkdocs documentation
140
+ /site
141
+
142
+ # mypy
143
+ .mypy_cache/
144
+ .dmypy.json
145
+ dmypy.json
146
+
147
+ # Pyre type checker
148
+ .pyre/
149
+
150
+ # pytype static type analyzer
151
+ .pytype/
152
+
153
+ # Cython debug symbols
154
+ cython_debug/
155
+
156
+ # PyCharm
157
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
160
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
161
+ #.idea/
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+
5
+ openai.api_key = os.environ['key']
6
+
7
+ model_name = "ft:gpt-3.5-turbo-0613:metric-space-ug:rechtsberatung2:7zmeC6ps"
8
+ prompt0 = "Liegt ein rechtlich relevanter Sachverhalt mit einer passenden Frage vor?"
9
+ prompt1 = "Du stellst Fragen, um fehlende Informationen für eine anwaltliche Erstberatung zu klären. Stelle bis zu 10 Fragen für die Klärung der Sachlage und antworte mit „Danke für die Antworten.“, sobald die Sachlage klar ist."
10
+ prompt2 = "Schreibe den Sachverhalt auf Basis der folgenden Informationen zusammen."
11
+ prompt3 = "Schreibe eine anwaltliche Erstberatung auf Basis des geschilderten Sachverhalts."
12
+
13
+ def respond_prompt0(Selbstauskunft, Kernfrage, chat):
14
+ # preprocess data
15
+
16
+ # openai api call prompt 0
17
+ ########
18
+ completion = openai.ChatCompletion.create(
19
+ model=model_name,
20
+ messages=[
21
+ {"role": "system", "content": prompt0},
22
+ {"role": "user", "content": Selbstauskunft + "\n" + Kernfrage}
23
+ ])
24
+ output = completion.choices[0].message.content
25
+ ########
26
+
27
+ out = "Liegt ein rechtlich relevanter Sachverhalt mit einer passenden Frage vor?\n" + output
28
+ if "ja" in output[:4].lower():
29
+ out += "\n Bitte beantworten Sie die folgenden Fragen um den Sachverhalt genauer zu erklären" #
30
+
31
+ # openai api call prompt 1
32
+ ########
33
+ completion = openai.ChatCompletion.create(
34
+ model=model_name,
35
+ messages=[
36
+ {"role": "system", "content": prompt1},
37
+ {"role": "user", "content": Selbstauskunft + "\n" + Kernfrage}
38
+ ])
39
+ output_prompt1 = completion.choices[0].message.content
40
+ ########
41
+
42
+ chat = [(output_prompt1, "")]
43
+ return out, gr.update(value=chat, visible=True), gr.update(value="", visible=True)
44
+
45
+ else:
46
+ return out + "\nLeider könne wir Ihnen nicht weiterhelfen", chat, ""
47
+
48
+
49
+ def respond_prompt1(Selbstauskunft, Kernfrage, chat, text):
50
+ chat[-1][1] = text
51
+ # openai api call prompt 1
52
+ ########
53
+ messages=[
54
+ {"role": "system", "content": prompt1},
55
+ {"role": "user", "content": Selbstauskunft + "\n" + Kernfrage}
56
+ ]
57
+
58
+ for i in range(len(chat)):
59
+ messages.append({ "role": "assistant", "content": chat[i][0]})
60
+ messages.append({ "role": "user", "content": chat[i][1] })
61
+
62
+ completion = openai.ChatCompletion.create(
63
+ model=model_name,
64
+ messages=messages)
65
+
66
+ output_prompt1 = completion.choices[0].message.content
67
+ ########
68
+
69
+
70
+ if ("<ENDOFQA>" in output_prompt1) or len(chat) > 10:
71
+ sachverhalt = respond_prompt2(Selbstauskunft, Kernfrage, chat)
72
+ chat.append(("Danke für die Antworten. Im folgendem Text finden sie eine Zusamenfassung des Sachverhalts", ""))
73
+
74
+ return chat, gr.update(value="", visible=False), gr.update(value=sachverhalt, visible=True), gr.update(value="Erstberatung generieren", visible=True)
75
+
76
+ chat.append((output_prompt1, ""))
77
+
78
+ return chat, "", "", ""
79
+
80
+ def respond_prompt2(Selbstauskunft, Kernfrage, chat):
81
+ # openai api call prompt 2
82
+ ########
83
+ content = Selbstauskunft + "\n" + Kernfrage
84
+
85
+ for i in range(len(chat)-1):
86
+ content += "\n\n" + chat[i][0] + "\n"
87
+ content += chat[i][1]
88
+
89
+ completion = openai.ChatCompletion.create(
90
+ model=model_name,
91
+ messages=[
92
+ { "role": "system", "content": prompt2 },
93
+ { "role": "user", "content": content}])
94
+
95
+ output_prompt2 = completion.choices[0].message.content
96
+ ########
97
+
98
+ return output_prompt2
99
+
100
+ def respond_prompt3(Selbstauskunft, Kernfrage, chat, Sachverhalt):
101
+ # openai api call prompt 3
102
+ ########
103
+ content = Selbstauskunft + "\n" + Kernfrage
104
+
105
+ for i in range(len(chat)-1):
106
+ content += "\n\n" + chat[i][0] + "\n"
107
+ content += chat[i][1]
108
+
109
+ completion = openai.ChatCompletion.create(
110
+ model=model_name,
111
+ messages=[
112
+ { "role": "system", "content": prompt3 },
113
+ { "role": "user", "content": content + "\nSachverhalt: " + Sachverhalt}])
114
+
115
+
116
+ output_prompt3 =completion.choices[0].message.content
117
+ ########
118
+
119
+ return gr.update(value=output_prompt3, visible=True), gr.update(visible=False)
120
+
121
+ with gr.Blocks() as demo:
122
+ Selbstauskunft = gr.Textbox("", label="Schildern sie das Problem")
123
+ Kernfrage = gr.Textbox("", label="Stellen sie ihre Frage")
124
+
125
+ button_prompt1 = gr.Button(value="Submit")
126
+
127
+ label_prompt0 = gr.Label()
128
+
129
+
130
+ chat = gr.Chatbot(visible=False)
131
+ msg = gr.Textbox(visible=False, label="Antwort")
132
+ Sachverhalt = gr.Textbox(visible=False, label = "Zusammenfassung")
133
+ Erstberatung = gr.Textbox(visible=False, label="Erstberatung")
134
+ button_erstberatung = gr.Button(value="Erstberatung generieren", visible=False)
135
+
136
+ button_prompt1.click(respond_prompt0, [Selbstauskunft, Kernfrage, chat], outputs=[label_prompt0, chat, msg])
137
+
138
+ msg.submit(respond_prompt1, [Selbstauskunft, Kernfrage, chat, msg], [chat, msg, Sachverhalt, button_erstberatung])
139
+
140
+ button_erstberatung.click(respond_prompt3, [Selbstauskunft, Kernfrage, chat, Sachverhalt], [Erstberatung, button_erstberatung])
141
+
142
+
143
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ aiohttp==3.8.5
3
+ aiosignal==1.3.1
4
+ altair==5.1.1
5
+ annotated-types==0.5.0
6
+ anyio==3.7.1
7
+ async-timeout==4.0.3
8
+ attrs==23.1.0
9
+ certifi==2023.7.22
10
+ charset-normalizer==3.2.0
11
+ click==8.1.7
12
+ contourpy==1.1.1
13
+ cycler==0.11.0
14
+ fastapi==0.103.1
15
+ ffmpy==0.3.1
16
+ filelock==3.12.4
17
+ fonttools==4.42.1
18
+ frozenlist==1.4.0
19
+ fsspec==2023.9.2
20
+ gradio==3.44.4
21
+ gradio_client==0.5.1
22
+ h11==0.14.0
23
+ httpcore==0.18.0
24
+ httpx==0.25.0
25
+ huggingface-hub==0.17.2
26
+ idna==3.4
27
+ importlib-resources==6.1.0
28
+ Jinja2==3.1.2
29
+ jsonschema==4.19.1
30
+ jsonschema-specifications==2023.7.1
31
+ kiwisolver==1.4.5
32
+ MarkupSafe==2.1.3
33
+ matplotlib==3.8.0
34
+ multidict==6.0.4
35
+ numpy==1.26.0
36
+ openai==0.28.1
37
+ orjson==3.9.7
38
+ packaging==23.1
39
+ pandas==2.1.1
40
+ Pillow==10.0.1
41
+ pydantic==2.4.0
42
+ pydantic_core==2.10.0
43
+ pydub==0.25.1
44
+ pyparsing==3.1.1
45
+ python-dateutil==2.8.2
46
+ python-multipart==0.0.6
47
+ pytz==2023.3.post1
48
+ PyYAML==6.0.1
49
+ referencing==0.30.2
50
+ requests==2.31.0
51
+ rpds-py==0.10.3
52
+ semantic-version==2.10.0
53
+ six==1.16.0
54
+ sniffio==1.3.0
55
+ starlette==0.27.0
56
+ toolz==0.12.0
57
+ tqdm==4.66.1
58
+ typing_extensions==4.8.0
59
+ tzdata==2023.3
60
+ urllib3==2.0.5
61
+ uvicorn==0.23.2
62
+ websockets==11.0.3
63
+ yarl==1.9.2