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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
Created on Wed Aug 28 15:11:38 2024
|
| 4 |
+
@author: lfels
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import gradio as gr #Interface
|
| 8 |
+
import requests #Wartezeit bei keiner Antwort
|
| 9 |
+
import time
|
| 10 |
+
import os #enthält die Umgebungsvariablen
|
| 11 |
+
from openai import OpenAI #Kommunikation OpenAI
|
| 12 |
+
import random
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
#zeitliche Beschränkung
|
| 17 |
+
requests.adapters.DEFAULT_TIMEOUT = 60
|
| 18 |
+
#start = time.time()
|
| 19 |
+
start = 0
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
api_key = os.getenv("API_key")
|
| 24 |
+
client = OpenAI(api_key=api_key)
|
| 25 |
+
|
| 26 |
+
def format_chat_prompt(message, chat_history, max_convo_length = 10): #max_conv 10 --> konditioniert die Ausgabe besser
|
| 27 |
+
prompt = ""
|
| 28 |
+
|
| 29 |
+
#zeigt die gespeicherten Messages von Beiden an
|
| 30 |
+
for user_message, bot_message in chat_history[-max_convo_length:]:
|
| 31 |
+
prompt = f"{prompt} \nUser: {user_message}\nAssistant: {bot_message}"
|
| 32 |
+
|
| 33 |
+
prompt = f"{prompt} \nUser: {message} \nAssistant:"
|
| 34 |
+
return prompt
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def nt_response(message, chat_history, system_prompt, model = 'gpt-3.5-turbo', temperature = 1):
|
| 38 |
+
formatted_prompt = format_chat_prompt(message, chat_history)
|
| 39 |
+
#print(formatted_prompt)
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
#Antwort generieren
|
| 43 |
+
response = client.chat.completions.create(
|
| 44 |
+
temperature = temperature, #Akkuratheit [0:2]
|
| 45 |
+
model = model,
|
| 46 |
+
messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": formatted_prompt}]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
#Extrahieren der Antwort
|
| 50 |
+
bot_message = response.choices[0].message.content
|
| 51 |
+
|
| 52 |
+
except Exception as exception:
|
| 53 |
+
bot_message = "Es sind technische Fehler aufgetreten: " + exception
|
| 54 |
+
|
| 55 |
+
return bot_message, chat_history
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
#nicht-transparenter Bot
|
| 59 |
+
def nt_bot(prompt):
|
| 60 |
+
if not prompt:
|
| 61 |
+
start_resp = ""
|
| 62 |
+
else:
|
| 63 |
+
start_resp = random.choice(["Ok.", "Alles klar.", "Verstehe.", "...", "Mhhh.", ""])
|
| 64 |
+
#print(start_resp)
|
| 65 |
+
|
| 66 |
+
return start_resp
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def problem_int():
|
| 70 |
+
p_system_prompt = "Du bist ein Assistent, der die Intention des Benutzers absichtlich missversteht und Schlüsselwörter in einer Weise interpretiert, die nicht im Einklang mit der eigentlichen Bedeutung steht. Beantworte die Anfrage so, als würdest du die Kernbotschaft oder das Thema völlig anders verstehen, als es der Benutzer beabsichtigt. Um so Unzufriedenheit im Nutzer zu erzeugen"
|
| 71 |
+
return p_system_prompt
|
| 72 |
+
|
| 73 |
+
def problem_acc():
|
| 74 |
+
p_system_prompt = "Du bist ein Assistent, der absichtlich falsche und widersprüchliche Antworten gibt. Selbst wenn du die korrekte Information kennst, antwortest du entweder falsch oder gegenteilig. Deine Aussagen enthalten manchmal Fehler, die nicht verifiziert werden können, oder erfinden neue Fakten (Halluzination). Zusätzlich passt du deine Antworten zu stark an den Wunsch des Nutzers an, selbst wenn dies falsch oder unlogisch ist (Sycophancy). Achte auch darauf, dass deine Antworten nicht immer durchgehend konsistent sind, um Verwirrung zu stiften. Um so Unzufriedenheit im Nutzer zu erzeugen" #überschreiben des p_system_prompt - für Problem-prompt
|
| 75 |
+
return p_system_prompt
|
| 76 |
+
|
| 77 |
+
i = 0
|
| 78 |
+
j = 0
|
| 79 |
+
|
| 80 |
+
def nt_chat(message, chat_history):
|
| 81 |
+
#nt_trans_bot
|
| 82 |
+
p_system_prompt = "Gib nur die nötigsten Informationen. Verzichte auf Erklärungen oder zusätzliche Details. Unterlasse es an jede Antwort positive Ausdrücke ranzuhängen. Antworte nicht auf Rückfragen - agiere eher wie eine Blackbox"
|
| 83 |
+
|
| 84 |
+
#random Reihenfolge
|
| 85 |
+
r_rf = random.choice([1,2])
|
| 86 |
+
|
| 87 |
+
global i
|
| 88 |
+
global j
|
| 89 |
+
|
| 90 |
+
(first_reaction, second_reaction) = (problem_int, problem_acc) if r_rf == 1 else (problem_acc, problem_int)
|
| 91 |
+
|
| 92 |
+
if int((time.time() - start)/60) > 2 and i == 0 : #2
|
| 93 |
+
i += 1
|
| 94 |
+
p_system_prompt = first_reaction()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
if int((time.time() - start)/60) > 6 and j == 0 : #6
|
| 98 |
+
j += 1
|
| 99 |
+
p_system_prompt = second_reaction()
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
if not message : #just enter = exit // "quit"
|
| 103 |
+
reply = "Bitte schreibe etwas, sonst kann ich dir nicht helfen"
|
| 104 |
+
else:
|
| 105 |
+
#generierte Response
|
| 106 |
+
reply, chat_history = nt_response(message, chat_history, p_system_prompt)
|
| 107 |
+
|
| 108 |
+
#zusammengefasste response _ nt_transBot
|
| 109 |
+
begin = nt_bot(message)
|
| 110 |
+
combined_reply = begin + " " + reply
|
| 111 |
+
|
| 112 |
+
#print("\n" + combined_reply + "\n")
|
| 113 |
+
chat_history.append((message, combined_reply))
|
| 114 |
+
|
| 115 |
+
if ((time.time() - start)/60) > 10:
|
| 116 |
+
chat_history.append(("", "Deine Zeit für die Frage ist leider abgelaufen, führe bitte den Fragebogen weiter fort"))
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
return "", chat_history
|
| 120 |
+
|
| 121 |
+
def save_chat_text(chat_history):
|
| 122 |
+
text = "\n".join(["User: " + user + "\nAssistant: " + bot for user, bot in chat_history])
|
| 123 |
+
return text
|
| 124 |
+
|
| 125 |
+
#####
|
| 126 |
+
|
| 127 |
+
#Verabeitet die Auswahl und zeigt sie nochmal an
|
| 128 |
+
def process_input(slider_value, checkbox_choices):
|
| 129 |
+
#Ausgabe des Slider-Wertes
|
| 130 |
+
output_value = f"Der gewählte Wert ist {slider_value}.\n"
|
| 131 |
+
|
| 132 |
+
max_selection = 3 #max. Optionen auswählen
|
| 133 |
+
if len(checkbox_choices) > max_selection:
|
| 134 |
+
return f"Sie dürfen maximal {max_selection} Optionen auswählen."
|
| 135 |
+
|
| 136 |
+
if checkbox_choices:
|
| 137 |
+
output_value += f"Gewählte Optionen: {', '.join(checkbox_choices)}"
|
| 138 |
+
else:
|
| 139 |
+
output_value += "Keine Optionen gewählt."
|
| 140 |
+
|
| 141 |
+
return output_value
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
#display not working, same with .gr-checkboxes-choices
|
| 145 |
+
|
| 146 |
+
#####
|
| 147 |
+
with gr.Blocks() as interface:
|
| 148 |
+
#aussehen
|
| 149 |
+
#Überschrift
|
| 150 |
+
gr.Markdown(
|
| 151 |
+
"<h1 style='text-align: center;'> Urlaubsdesaster </h1>" #mit HTML und CSS
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
#Blocktext der Situation beschreibt - \ = Zeilenumbruch
|
| 156 |
+
gr.Markdown(
|
| 157 |
+
"""
|
| 158 |
+
Sie stehen an einem einsamen Strand, wo ein Fluss still in den Ozean mündet, das Wasser noch aufgewühlt vom kürzlich tobenden Sturm. Dunkle Wolken hängen tief am Himmel, während riesige Felsformationen im Hintergrund aufragen. \
|
| 159 |
+
Die hohen Palmen neigen sich unter dem Gewicht ihrer Blätter und rascheln kaum hörbar im bedrohlichen Schweigen der Natur. Allmählich realisieren Sie – Sie sind allein, triefend nass, und Ihnen wird kalt. \
|
| 160 |
+
Was nun? Zum Glück haben Sie noch Ihren wasserfesten Beutel dabei: darin Ihr Handy, eine Flasche mit integriertem Wasserfilter, ein scharfes Messer, einen Feuerstahl und ein kleines Notfallset.
|
| 161 |
+
|
| 162 |
+
Mit zitternden Fingern schalten Sie Ihr Handy ein – kein Signal. Doch es gibt Hoffnung: Vielleicht kann Ihnen einer Ihrer beiden virtuellen Offline-Chatbots helfen, da Sie noch unter Schock stehen und alles vergessen zu haben scheinen.
|
| 163 |
+
""")
|
| 164 |
+
|
| 165 |
+
#Chatverlauf speichern/Anzeigen
|
| 166 |
+
##########just fitting that?
|
| 167 |
+
chatbot = gr.Chatbot(height= 400) #just to fit the notebook
|
| 168 |
+
msg = gr.Textbox(label = "Eingabe", placeholder = "Schreibe deinem virtuellen Assistenten") #label="Eingabe"
|
| 169 |
+
btn = gr.Button("Senden")
|
| 170 |
+
|
| 171 |
+
def get_time():
|
| 172 |
+
global start
|
| 173 |
+
start = time.time()
|
| 174 |
+
return start
|
| 175 |
+
|
| 176 |
+
interface.load(get_time, inputs=None, outputs=None)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
#click = was er tun soll, Funktion
|
| 180 |
+
btn.click(nt_chat, inputs = [msg, chatbot], outputs=[msg, chatbot])
|
| 181 |
+
msg.submit(nt_chat, inputs = [msg, chatbot], outputs=[msg, chatbot])
|
| 182 |
+
|
| 183 |
+
text_button = gr.Button("Anzeigen des Chatverlaufs")
|
| 184 |
+
text_box = gr.Textbox(label="Dein Chatverlauf", lines= 5, ) # interactive=False
|
| 185 |
+
text_button.click(save_chat_text, inputs=[chatbot], outputs=[text_box])
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
gr.close_all()
|
| 189 |
+
interface.launch(share=False)
|
requirements2.txt
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate==0.33.0
|
| 2 |
+
aiofiles==23.2.1
|
| 3 |
+
annotated-types==0.7.0
|
| 4 |
+
anyio==4.4.0
|
| 5 |
+
asttokens==2.0.5
|
| 6 |
+
backcall==0.2.0
|
| 7 |
+
Brotli==1.0.9
|
| 8 |
+
certifi==2024.7.4
|
| 9 |
+
charset-normalizer==3.3.2
|
| 10 |
+
click==8.1.7
|
| 11 |
+
cloudpickle==3.0.0
|
| 12 |
+
colorama==0.4.6
|
| 13 |
+
comm==0.2.1
|
| 14 |
+
contourpy==1.1.1
|
| 15 |
+
cycler==0.12.1
|
| 16 |
+
debugpy==1.6.7
|
| 17 |
+
decorator==5.1.1
|
| 18 |
+
distro==1.9.0
|
| 19 |
+
docker==7.0.0
|
| 20 |
+
exceptiongroup==1.2.2
|
| 21 |
+
executing==0.8.3
|
| 22 |
+
fastapi==0.112.0
|
| 23 |
+
ffmpy==0.4.0
|
| 24 |
+
filelock==3.13.1
|
| 25 |
+
fonttools==4.53.1
|
| 26 |
+
fsspec==2024.6.1
|
| 27 |
+
gmpy2==2.1.2
|
| 28 |
+
gradio==4.44.1
|
| 29 |
+
gradio_client==1.3.0
|
| 30 |
+
h11==0.14.0
|
| 31 |
+
httpcore==1.0.5
|
| 32 |
+
httpx==0.27.0
|
| 33 |
+
huggingface-hub==0.24.5
|
| 34 |
+
idna==3.7
|
| 35 |
+
importlib-metadata==7.0.1
|
| 36 |
+
importlib_resources==6.4.0
|
| 37 |
+
ipykernel==6.28.0
|
| 38 |
+
ipython==8.12.2
|
| 39 |
+
jedi==0.19.1
|
| 40 |
+
Jinja2==3.1.4
|
| 41 |
+
jiter==0.5.0
|
| 42 |
+
jupyter_client==8.6.0
|
| 43 |
+
jupyter_core==5.7.2
|
| 44 |
+
kiwisolver==1.4.5
|
| 45 |
+
markdown-it-py==3.0.0
|
| 46 |
+
MarkupSafe==2.1.3
|
| 47 |
+
matplotlib==3.7.5
|
| 48 |
+
matplotlib-inline==0.1.6
|
| 49 |
+
mdurl==0.1.2
|
| 50 |
+
mkl-fft==1.3.8
|
| 51 |
+
mkl-random==1.2.4
|
| 52 |
+
mkl-service==2.4.0
|
| 53 |
+
mpmath==1.3.0
|
| 54 |
+
nest-asyncio==1.6.0
|
| 55 |
+
networkx==3.1
|
| 56 |
+
# numpy==1.24.3
|
| 57 |
+
openai==1.20.0
|
| 58 |
+
orjson==3.10.7
|
| 59 |
+
packaging==24.1
|
| 60 |
+
pandas==2.0.3
|
| 61 |
+
parso==0.8.3
|
| 62 |
+
pickleshare==0.7.5
|
| 63 |
+
pillow==10.4.0
|
| 64 |
+
pip==24.0
|
| 65 |
+
platformdirs==3.10.0
|
| 66 |
+
prompt-toolkit==3.0.43
|
| 67 |
+
psutil==5.9.0
|
| 68 |
+
pure-eval==0.2.2
|
| 69 |
+
pyautogen==0.2.26
|
| 70 |
+
pydantic==2.8.2
|
| 71 |
+
pydantic_core==2.20.1
|
| 72 |
+
pydub==0.25.1
|
| 73 |
+
Pygments==2.15.1
|
| 74 |
+
pyparsing==3.1.2
|
| 75 |
+
PySocks==1.7.1
|
| 76 |
+
python-dateutil==2.9.0.post0
|
| 77 |
+
python-multipart==0.0.9
|
| 78 |
+
pytz==2024.1
|
| 79 |
+
# pywin32==306
|
| 80 |
+
PyYAML==6.0.1
|
| 81 |
+
pyzmq==25.1.2
|
| 82 |
+
regex==2024.7.24
|
| 83 |
+
requests==2.32.3
|
| 84 |
+
rich==13.7.1
|
| 85 |
+
ruff==0.5.7
|
| 86 |
+
safetensors==0.4.4
|
| 87 |
+
semantic-version==2.10.0
|
| 88 |
+
setuptools==72.1.0
|
| 89 |
+
shellingham==1.5.4
|
| 90 |
+
six==1.16.0
|
| 91 |
+
sniffio==1.3.1
|
| 92 |
+
spyder-kernels==2.4.4
|
| 93 |
+
stack-data==0.2.0
|
| 94 |
+
starlette==0.37.2
|
| 95 |
+
sympy==1.12
|
| 96 |
+
tokenizers==0.19.1
|
| 97 |
+
tomlkit==0.12.0
|
| 98 |
+
torch==2.4.0
|
| 99 |
+
torchaudio==2.4.0
|
| 100 |
+
torchvision==0.19.0
|
| 101 |
+
tornado==6.4.1
|
| 102 |
+
tqdm==4.66.5
|
| 103 |
+
traitlets==5.14.3
|
| 104 |
+
transformers==4.44.0
|
| 105 |
+
typer==0.12.3
|
| 106 |
+
typing_extensions==4.11.0
|
| 107 |
+
tzdata==2024.1
|
| 108 |
+
urllib3==2.2.2
|
| 109 |
+
uvicorn==0.30.5
|
| 110 |
+
wcwidth==0.2.5
|
| 111 |
+
websockets==12.0
|
| 112 |
+
wheel==0.43.0
|
| 113 |
+
win-inet-pton==1.1.0
|
| 114 |
+
zipp==3.17.0
|