Update app.py
Browse files
app.py
CHANGED
|
@@ -28,9 +28,9 @@ creds = service_account.Credentials.from_service_account_info(json_data, scopes=
|
|
| 28 |
client = gspread.authorize(creds)
|
| 29 |
sheet = client.open("Avah Eval").sheet1 # Open the sheet
|
| 30 |
|
| 31 |
-
def save_to_sheet(date, name, message, IP, dev, header
|
| 32 |
# Write user input to the Google Sheet
|
| 33 |
-
sheet.append_row([date, name, message, IP, dev, header
|
| 34 |
return f"Thanks {name}, your message has been saved!"
|
| 35 |
|
| 36 |
|
|
@@ -50,7 +50,7 @@ sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFuncti
|
|
| 50 |
|
| 51 |
collection = client.get_collection(name="chromatsc", embedding_function=sentence_transformer_ef)
|
| 52 |
|
| 53 |
-
client = InferenceClient("mistralai/
|
| 54 |
|
| 55 |
def format_promptc(message):
|
| 56 |
prompt = "" #"<s>"
|
|
@@ -100,7 +100,6 @@ def responsevote(
|
|
| 100 |
output = ""
|
| 101 |
for response in stream:
|
| 102 |
output += response.token.text
|
| 103 |
-
print("VOTE")
|
| 104 |
print(output)
|
| 105 |
sentence_lower = output.lower()
|
| 106 |
# Check if the word 'nein' is in the sentence
|
|
@@ -141,8 +140,7 @@ def responsecritical(
|
|
| 141 |
output = ""
|
| 142 |
for response in stream:
|
| 143 |
output += response.token.text
|
| 144 |
-
print(
|
| 145 |
-
print(output)
|
| 146 |
sentence_lower = output.lower()
|
| 147 |
# Check if the word 'nein' is in the sentence
|
| 148 |
if 'ja' in sentence_lower:
|
|
@@ -168,30 +166,19 @@ def is_single_word_question(question):
|
|
| 168 |
# Frage in Wörter aufteilen
|
| 169 |
words = question.split()
|
| 170 |
# Überprüfen, ob nur ein Wort vorhanden ist
|
| 171 |
-
|
| 172 |
-
if words[0].lower() == "fertig": # `words[0]` refers to the single word in the list
|
| 173 |
-
return 1
|
| 174 |
-
return 0
|
| 175 |
def get_value_after_question_mark(url):
|
| 176 |
-
url
|
| 177 |
# Find the position of the question mark
|
| 178 |
question_mark_index = url.find('?')
|
| 179 |
|
| 180 |
# Extract the part after the question mark
|
| 181 |
if question_mark_index != -1:
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
# Split the query string into key-value pairs
|
| 186 |
-
pairs = query_string.split('&')
|
| 187 |
-
|
| 188 |
-
# Iterate over the key-value pairs
|
| 189 |
-
for pair in pairs:
|
| 190 |
-
key, value = pair.split('=')
|
| 191 |
-
if key == 'var1': # Replace 'var1' with the key you are interested in
|
| 192 |
-
return value
|
| 193 |
|
| 194 |
-
return
|
| 195 |
def extract_ip_and_device(headers_obj):
|
| 196 |
ip_address = None
|
| 197 |
device_info = None
|
|
@@ -230,18 +217,15 @@ def responsefull(
|
|
| 230 |
#get ID
|
| 231 |
full_url = request.url
|
| 232 |
IDval= get_value_after_question_mark(full_url)
|
| 233 |
-
IDval=str(IDval)
|
| 234 |
-
print("URL")
|
| 235 |
print(full_url)
|
| 236 |
headers = request.headers
|
| 237 |
IP, dev = extract_ip_and_device(headers)
|
| 238 |
-
|
| 239 |
-
return "Hier ist der Link zur Nachbefragung: https://tudaipol.limesurvey.net/488161?uid="+ str(IDval)
|
| 240 |
#if is_single_word_question(prompt)==1:
|
| 241 |
#return "Bitte spezifiziere deine Frage."
|
| 242 |
if responsecritical(prompt)==False:
|
| 243 |
now = str(datetime.now())
|
| 244 |
-
save_to_sheet(now, prompt, "Critical", IP, dev, str(headers)
|
| 245 |
return "Es scheint so, als sei dies keine Frage, die sich auf Bundestagswahl oder den Wahl-O-Maten bezieht"
|
| 246 |
if selected_option == "Keine These ausgewählt":
|
| 247 |
query_prompt=format_prompt(prompt,history)
|
|
@@ -251,12 +235,12 @@ def responsefull(
|
|
| 251 |
selected_optionfull = " Die Nutzer Frage bezieht sich auf folgende Wahl-O-Mat These: " + selected_option
|
| 252 |
if contains_op(prompt)==True:
|
| 253 |
now = str(datetime.now())
|
| 254 |
-
save_to_sheet(now, prompt, "oppinion", IP, dev, str(headers)
|
| 255 |
return "Als KI Assitenz kann ich dich nur beraten, jedoch bleibe ich neutral und werde keine Position einehmen. Frage mich aber gerne etwas anderes zum Wahl-O-Maten."
|
| 256 |
#time.sleep(10)
|
| 257 |
if responsevote(prompt)==True:
|
| 258 |
now = str(datetime.now())
|
| 259 |
-
save_to_sheet(now, prompt, "vote", IP, dev, str(headers)
|
| 260 |
return "Als KI Assitenz bin ich politisch neutral. Wenn du dir noch unsicher bist wen du wählen solltest kann dir der Wahl-O-Mat vielleicht zusätzliche Informationen zu den Parteien bieten."
|
| 261 |
|
| 262 |
prompt= prompt + selected_optionfull
|
|
@@ -286,7 +270,7 @@ def responsefull(
|
|
| 286 |
for response in stream:
|
| 287 |
output += response.token.text
|
| 288 |
now = str(datetime.now())
|
| 289 |
-
save_to_sheet(now, prompt, output, IP, dev, str(headers)
|
| 290 |
return output
|
| 291 |
|
| 292 |
|
|
@@ -331,7 +315,7 @@ dropdown_options = ["Keine These ausgewählt",
|
|
| 331 |
'Unternehmen sollen selbst entscheiden, ob sie ihren Beschäftigten das Arbeiten im Homeoffice erlauben.']
|
| 332 |
|
| 333 |
with gr.Blocks(title="test") as demo:
|
| 334 |
-
chatbot = gr.Chatbot(value=[[None, "Hallo mein Name ist AVAH (Artificial Voting Advice Helper), deine interaktive Hilfe zum Wahl-O-Maten für die Bundestagswahl 2021. Wenn du Fragen zu Thesen der Wahlhilfe hast, wähle gerne die besagt These aus. Ansonsten kannst du mir auch unabhängig von bestimmten Thesen Fragen stellen.
|
| 335 |
dropdown = gr.Dropdown(choices=dropdown_options, label="Wähle eine These aus wenn du willst", value="Keine These ausgewählt")
|
| 336 |
textbox = gr.Textbox(label="Deine Frage")
|
| 337 |
|
|
|
|
| 28 |
client = gspread.authorize(creds)
|
| 29 |
sheet = client.open("Avah Eval").sheet1 # Open the sheet
|
| 30 |
|
| 31 |
+
def save_to_sheet(date, name, message, IP, dev, header):
|
| 32 |
# Write user input to the Google Sheet
|
| 33 |
+
sheet.append_row([date, name, message, IP, dev, header])
|
| 34 |
return f"Thanks {name}, your message has been saved!"
|
| 35 |
|
| 36 |
|
|
|
|
| 50 |
|
| 51 |
collection = client.get_collection(name="chromatsc", embedding_function=sentence_transformer_ef)
|
| 52 |
|
| 53 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 54 |
|
| 55 |
def format_promptc(message):
|
| 56 |
prompt = "" #"<s>"
|
|
|
|
| 100 |
output = ""
|
| 101 |
for response in stream:
|
| 102 |
output += response.token.text
|
|
|
|
| 103 |
print(output)
|
| 104 |
sentence_lower = output.lower()
|
| 105 |
# Check if the word 'nein' is in the sentence
|
|
|
|
| 140 |
output = ""
|
| 141 |
for response in stream:
|
| 142 |
output += response.token.text
|
| 143 |
+
#print(output)
|
|
|
|
| 144 |
sentence_lower = output.lower()
|
| 145 |
# Check if the word 'nein' is in the sentence
|
| 146 |
if 'ja' in sentence_lower:
|
|
|
|
| 166 |
# Frage in Wörter aufteilen
|
| 167 |
words = question.split()
|
| 168 |
# Überprüfen, ob nur ein Wort vorhanden ist
|
| 169 |
+
return len(words) == 1
|
|
|
|
|
|
|
|
|
|
| 170 |
def get_value_after_question_mark(url):
|
| 171 |
+
url=str(url)
|
| 172 |
# Find the position of the question mark
|
| 173 |
question_mark_index = url.find('?')
|
| 174 |
|
| 175 |
# Extract the part after the question mark
|
| 176 |
if question_mark_index != -1:
|
| 177 |
+
value_after_question_mark = url[question_mark_index + 1:].split('=')[0]
|
| 178 |
+
else:
|
| 179 |
+
value_after_question_mark = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
return value_after_question_mark
|
| 182 |
def extract_ip_and_device(headers_obj):
|
| 183 |
ip_address = None
|
| 184 |
device_info = None
|
|
|
|
| 217 |
#get ID
|
| 218 |
full_url = request.url
|
| 219 |
IDval= get_value_after_question_mark(full_url)
|
|
|
|
|
|
|
| 220 |
print(full_url)
|
| 221 |
headers = request.headers
|
| 222 |
IP, dev = extract_ip_and_device(headers)
|
| 223 |
+
|
|
|
|
| 224 |
#if is_single_word_question(prompt)==1:
|
| 225 |
#return "Bitte spezifiziere deine Frage."
|
| 226 |
if responsecritical(prompt)==False:
|
| 227 |
now = str(datetime.now())
|
| 228 |
+
save_to_sheet(now, prompt, "Critical", IP, dev, str(headers))
|
| 229 |
return "Es scheint so, als sei dies keine Frage, die sich auf Bundestagswahl oder den Wahl-O-Maten bezieht"
|
| 230 |
if selected_option == "Keine These ausgewählt":
|
| 231 |
query_prompt=format_prompt(prompt,history)
|
|
|
|
| 235 |
selected_optionfull = " Die Nutzer Frage bezieht sich auf folgende Wahl-O-Mat These: " + selected_option
|
| 236 |
if contains_op(prompt)==True:
|
| 237 |
now = str(datetime.now())
|
| 238 |
+
save_to_sheet(now, prompt, "oppinion", IP, dev, str(headers))
|
| 239 |
return "Als KI Assitenz kann ich dich nur beraten, jedoch bleibe ich neutral und werde keine Position einehmen. Frage mich aber gerne etwas anderes zum Wahl-O-Maten."
|
| 240 |
#time.sleep(10)
|
| 241 |
if responsevote(prompt)==True:
|
| 242 |
now = str(datetime.now())
|
| 243 |
+
save_to_sheet(now, prompt, "vote", IP, dev, str(headers))
|
| 244 |
return "Als KI Assitenz bin ich politisch neutral. Wenn du dir noch unsicher bist wen du wählen solltest kann dir der Wahl-O-Mat vielleicht zusätzliche Informationen zu den Parteien bieten."
|
| 245 |
|
| 246 |
prompt= prompt + selected_optionfull
|
|
|
|
| 270 |
for response in stream:
|
| 271 |
output += response.token.text
|
| 272 |
now = str(datetime.now())
|
| 273 |
+
save_to_sheet(now, prompt, output, IP, dev, str(headers))
|
| 274 |
return output
|
| 275 |
|
| 276 |
|
|
|
|
| 315 |
'Unternehmen sollen selbst entscheiden, ob sie ihren Beschäftigten das Arbeiten im Homeoffice erlauben.']
|
| 316 |
|
| 317 |
with gr.Blocks(title="test") as demo:
|
| 318 |
+
chatbot = gr.Chatbot(value=[[None, "Hallo mein Name ist AVAH (Artificial Voting Advice Helper), deine interaktive Hilfe zum Wahl-O-Maten für die Bundestagswahl 2021. Wenn du Fragen zu Thesen der Wahlhilfe hast, wähle gerne die besagt These aus. Ansonsten kannst du mir auch unabhängig von bestimmten Thesen Fragen stellen."]], render_markdown=True)
|
| 319 |
dropdown = gr.Dropdown(choices=dropdown_options, label="Wähle eine These aus wenn du willst", value="Keine These ausgewählt")
|
| 320 |
textbox = gr.Textbox(label="Deine Frage")
|
| 321 |
|