mgokg commited on
Commit
c935ce9
·
verified ·
1 Parent(s): a94f470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +542 -28
app.py CHANGED
@@ -37,40 +37,554 @@ def get_prompt(prompt):
37
 
38
  def show_html():
39
  return """<!DOCTYPE html>
40
- <html lang="en">
41
  <head>
42
  <meta charset="UTF-8">
43
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
44
- <title>Textfeld mit FastAPI</title>
45
- </head>
46
- <body>
47
- <h1>Textfeld mit FastAPI</h1>
48
- <form id="myForm">
49
- <label for="prompt">Prompt:</label>
50
- <input type="text" id="prompt" name="prompt">
51
- <button type="button" onclick="sendData()">Senden</button>
52
- </form>
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  <script>
55
- async function sendData() {
56
- const prompt = document.getElementById("prompt").value;
57
- const zeitstempel = Math.floor(Date.now() / 1000); // Unix-Zeitstempel
58
- const data = {
59
- prompt: prompt,
60
- zeitstempel: zeitstempel
61
- };
62
- const response = await fetch("https://mgokg-fastapi.hf.space:8000/items", {
63
- method: "POST",
64
- headers: {
65
- "Content-Type": "application/json"
66
- },
67
- body: JSON.stringify(data)
68
- });
69
- const result = await response.json();
70
- alert(result)
71
- console.log(result)
72
  }
 
73
  </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  </body>
75
  </html>
76
  """
 
37
 
38
  def show_html():
39
  return """<!DOCTYPE html>
40
+ <html lang="de">
41
  <head>
42
  <meta charset="UTF-8">
43
+ <meta name="viewport" content="width=device-width, initial-scale=1">
44
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
45
+ <meta http-equiv="cache-control" content="no-cache">
46
+ <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Roboto+Slab:400,700|Material+Icons" />
47
+ <link rel="icon" type="image/vnd.microsoft.icon" href="favicon.ico">
48
+ <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
49
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
50
+ <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3pro.css">
51
+ <link rel="stylesheet" href="style.css">
52
+ <script type="module" src="https://md-block.verou.me/md-block.js"></script>
53
+ <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
54
 
55
+ <title>Chatbot-Interface</title>
56
+ <style>
57
+ body {
58
+ background-color: #121212;
59
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
60
+ height: 100vh;
61
+ margin: 0;
62
+ display: flex;
63
+ flex-direction: column;
64
+ justify-content: space-between;
65
+ }
66
+ .chatbot-container {
67
+ width: 380px;
68
+ flex: 1;
69
+ display: flex;
70
+ flex-direction: column;
71
+ background-color: black;
72
+ padding: 10px;
73
+ box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
74
+ margin-bottom: 10px;
75
+ border-radius: 25px;
76
+ border: 2px solid #121212;
77
+ }
78
+ .chatbot-header {
79
+ background-color: black;
80
+ padding: 10px;
81
+ border-bottom: 1px solid #666;
82
+ }
83
+ .chatbot-header h1 {
84
+ color: #fff;
85
+ margin: 0;
86
+ }
87
+ .chatbot-messages {
88
+ padding: 10px;
89
+ flex: 1;
90
+ overflow-y: auto;
91
+ }
92
+ #messages {
93
+ list-style: none;
94
+ padding: 0;
95
+ margin: 0;
96
+ }
97
+ #messages li {
98
+ padding: 10px;
99
+ border-bottom: 1px solid #161616;
100
+ background-color: #161616;
101
+ color: white;
102
+ }
103
+ #messages li:last-child {
104
+ border-bottom: none;
105
+ }
106
+ .chatbot-input {
107
+ padding: 10px;
108
+ background-color: #121212;
109
+ border-radius: 15px;
110
+ position: sticky;
111
+ bottom: 0;
112
+ z-index: 10;
113
+ }
114
+ .btn {
115
+ color: white;
116
+ width: 50px;
117
+ height: 50px;
118
+ background: #121212;
119
+ display: flex;
120
+ justify-content: center;
121
+ align-items: center;
122
+ float: right;
123
+ box-shadow: var(--box-shadow);
124
+ cursor: pointer;
125
+ transition: all 0.3s ease;
126
+ }
127
+ .btn:hover {
128
+ background: #444;
129
+ }
130
+ #input {
131
+ width: 100%;
132
+ height: 60px;
133
+ padding: 15px;
134
+ font-size: 16px;
135
+ border: none;
136
+ border-radius: 5px;
137
+ background-color: #282828;
138
+ color: white;
139
+ }
140
+ #senden {
141
+ width: 20%;
142
+ height: 30px;
143
+ padding: 0px;
144
+ font-size: 16px;
145
+ border: none;
146
+ background-color: #484848;
147
+ color: #fff;
148
+ cursor: pointer;
149
+ border-bottom: 1px solid #666;
150
+ float: right;
151
+ }
152
+ #senden:hover {
153
+ background-color: #666;
154
+ }
155
+ .icon-wrapper {
156
+ width: 50px;
157
+ height: 50px;
158
+ background: var(--background-color);
159
+ border-radius: 50%;
160
+ display: flex;
161
+ align-items: center;
162
+ justify-content: center;
163
+ position: relative;
164
+ box-shadow: var(--box-shadow);
165
+ cursor: pointer;
166
+ transition: all 0.3s ease;
167
+ }
168
+ #appleImage {
169
+ display: none;
170
+ }
171
+ .search-bar {
172
+ display: flex;
173
+ align-items: center;
174
+ background-color: #282828;
175
+ padding: 10px 20px;
176
+ width: 100%;
177
+ border-radius: 25px;
178
+ }
179
+ .search-bar textarea {
180
+ width: 100%;
181
+ border: none;
182
+ background-color: #121212;
183
+ color: white;
184
+ outline: none;
185
+ flex: 1;
186
+ font-size: 18px;
187
+ }
188
+ .search-bar textarea::placeholder {
189
+ color: white;
190
+ }
191
+ .search-bar .icon {
192
+ background-color: #282828;
193
+ color: white;
194
+ font-size: 30px;
195
+ float: right;
196
+ }
197
+ .search-bar .icon:hover {
198
+ background: #444;
199
+ }
200
+ .search-bar .microphone {
201
+ background-color: #282828;
202
+ color: white;
203
+ font-size: 30px;
204
+ padding-right: 10px;
205
+ float: left;
206
+ }
207
+ .search-bar .speaker {
208
+ background-color: #282828;
209
+ color: white;
210
+ font-size: 30px;
211
+ float: left;
212
+ }
213
+ .search-bar .speaker:hover {
214
+ background: #444;
215
+ }
216
+ .container {
217
+ display: flex;
218
+ gap: 50px;
219
+ }
220
+ img {
221
+ object-fit: cover;
222
+ }
223
+ button {
224
+ background-color: var(--background-color);
225
+ border: none;
226
+ padding: 10px 20px;
227
+ font-size: 1em;
228
+ color: var(--heart-color);
229
+ cursor: pointer;
230
+ box-shadow: var(--box-shadow);
231
+ transition: all 0.3s ease-in-out;
232
+ }
233
+ .switch {
234
+ position: relative;
235
+ display: inline-block;
236
+ width: 60px;
237
+ height: 34px;
238
+ }
239
+ .switch input {
240
+ opacity: 0;
241
+ width: 0;
242
+ height: 0;
243
+ }
244
+ .slider {
245
+ position: absolute;
246
+ cursor: pointer;
247
+ top: 0;
248
+ left: 0;
249
+ right: 0;
250
+ bottom: 0;
251
+ background-color: grey;
252
+ -webkit-transition: .4s;
253
+ transition: .4s;
254
+ }
255
+ .slider:before {
256
+ position: absolute;
257
+ content: "";
258
+ height: 26px;
259
+ width: 26px;
260
+ left: 4px;
261
+ bottom: 4px;
262
+ background-color: white;
263
+ -webkit-transition: .4s;
264
+ transition: .4s;
265
+ }
266
+ input:checked + .slider {
267
+ background-color: orange;
268
+ }
269
+ input:focus + .slider {
270
+ box-shadow: 0 0 1px #2196F3;
271
+ }
272
+ input:checked + .slider:before {
273
+ -webkit-transform: translateX(26px);
274
+ -ms-transform: translateX(26px);
275
+ transform: translateX(26px);
276
+ }
277
+ .slider.round {
278
+ border-radius: 34px;
279
+ }
280
+ .slider.round:before {
281
+ border-radius: 50%;
282
+ }
283
+ input[type="range"] {
284
+ -webkit-appearance: none;
285
+ width: 100%;
286
+ height: 8px;
287
+ background: #ddd;
288
+ outline: none;
289
+ opacity: 0.7;
290
+ -webkit-transition: .2s;
291
+ transition: opacity .2s;
292
+ }
293
+ input[type="range"]::-webkit-slider-thumb {
294
+ -webkit-appearance: none;
295
+ appearance: none;
296
+ width: 25px;
297
+ height: 25px;
298
+ background: grey;
299
+ cursor: pointer;
300
+ border-radius: 50%;
301
+ }
302
+ input[type="range"]::-moz-range-thumb {
303
+ width: 25px;
304
+ height: 25px;
305
+ background: grey;
306
+ cursor: pointer;
307
+ border-radius: 50%;
308
+ }
309
+ .textborder {
310
+ color: #383838;
311
+ padding: 5px;
312
+ display: inline-block;
313
+ }
314
+ .text-outline {
315
+ font-size: 70px;
316
+ color: grey;
317
+ text-shadow: -3px -1px 0 #e08341;
318
+ }
319
+ </style>
320
+ <style>
321
+ #results-container {
322
+ display:none;
323
+ }
324
+ </style>
325
  <script>
326
+ /*
327
+ if (window.innerWidth <= 800) { // Überprüft die Breite des Bildschirms
328
+ window.location.href = "https://specialist-it.de/chatbot/mobile/"; // Leitet auf die mobile Version um
 
 
 
 
 
 
 
 
 
 
 
 
 
 
329
  }
330
+ */
331
  </script>
332
+ </head>
333
+ <body style="background-color:#121212;color:white">
334
+ <br>
335
+ <div class="w3-third">
336
+ <div class="chatbot-container" style="margin-top: 0px; margin-left: 0px;">
337
+ <div class="chatbot-header" style="padding-right: 10px">
338
+ <div class="w3-cell-row" style="">
339
+ <div class="w3-cell " style="">
340
+ <h1>Voicebot</h1>
341
+ </div>
342
+ <div class="w3-cell " style="color: white; background-color: black; float:right">
343
+ <center><img id="appleImage" src="apple.gif" alt="Apple" style="width: 100px;"></center>
344
+ </div>
345
+ </div>
346
+
347
+ </div>
348
+ <div class="chatbot-messages">
349
+ <center> <img id="ava" src="ava.png" style="width: 280px;"></center>
350
+
351
+ <div class="w3-cell-row" style="padding-left: 10px">
352
+ <div class="w3-cell w3-cell-bottom">
353
+ <div id="markdown" style="color:white; margin-left:0px"><b></b></div>
354
+ </div>
355
+ <div class="w3-cell w3-cell-top" style="color: white; background-color: black; padding: 8px; border-radius: 8px;height:">
356
+
357
+ </div>
358
+ </div>
359
+
360
+ <div class="w3-card-4 w3-container" style="width: 300px; border-radius:10px; margin-left:0px;">
361
+ <div class="w3-cell-row">
362
+ <div class="w3-cell">
363
+ <p><label for="language-select" style="color: white;">Sprache:</label></p>
364
+ <select id="language-select" style="width: 280px;">
365
+ <option value="de-DE">Deutsch</option>
366
+ <option value="en-US">Englisch (USA)</option>
367
+ <option value="fr-FR">Französisch</option>
368
+ <option value="it-IT">Italienisch</option>
369
+ </select>
370
+ </div>
371
+ </div>
372
+ <div class="w3-cell-row">
373
+ <div class="w3-cell">
374
+ <p><label for="voice-select" style="color: white; margin-left:0px">Stimme:</label></p>
375
+ <select id="voice-select" style="width: 280px; margin-left:0px"></select>
376
+ </div>
377
+ </div>
378
+ <div class="w3-cell-row">
379
+ <div class="w3-cell w3-cell-top">
380
+ <p><label style="color:white" for="rate-slider">Speaking Rate:</label></p>
381
+ <input style="background-color: orange;" type="range" id="rate-slider" min="0.7" max="1.3" step="0.1" value="1">
382
+ </div>
383
+ <div class="w3-cell" style="padding-left:40px">
384
+ <p><label for="pitch" style="color: white">Websuche</label></p>
385
+ <label class="switch">
386
+ <input style="padding-left:20px" class="w3-right" type="checkbox" id="pitch" checked>
387
+ <span class="slider round"></span>
388
+ </label>
389
+ </div>
390
+ </div>
391
+ <br>
392
+ </div>
393
+ </div>
394
+ <div class="chatbot-input" style="background:black;">
395
+ <div class="search-bar" style="width: 100%;">
396
+ <button id="recording" class="microphone btn">🎤</button>
397
+ <button id="stop" class="speaker btn">🔊</button>
398
+ <textarea id="input" rows="2" placeholder=" ask anything..."></textarea>
399
+ <button id="senden" class="icon " style="width:50px;height: 50px;border-radius:15px">⏎</button>
400
+ </div>
401
+ </div>
402
+ </div>
403
+ </div>
404
+
405
+ <div class="w3-container w3-twothird" style="font-size:24px; padding:20px">
406
+ <md-block>
407
+ <ul class="" id="messages" style="background:; color:white; height:600px; padding:20px">
408
+ <code><pre>
409
+
410
+ </pre></code>
411
+ </ul>
412
+ </md-block>
413
+ </div>
414
+
415
+ <div id="results-container">
416
+ <!-- Hier werden die Suchergebnisse angezeigt -->
417
+ <gcse:searchresults-only></gcse:searchresults-only>
418
+ </div>
419
+
420
+
421
+ <script async src="https://cse.google.com/cse.js?cx=77f1602c0ff764edb"></script>
422
+ <script>
423
+ const submitButton = document.getElementById('senden');
424
+ const promptInput = document.getElementById('input');
425
+ const messages = document.getElementById('messages');
426
+ const recording = document.getElementById('recording');
427
+ const stop = document.getElementById('stop');
428
+ const input = document.getElementById('input');
429
+ const toggle = document.getElementById('pitch');
430
+ const apple = document.getElementById('apple');
431
+ let recognition;
432
+ let x = "";
433
+ const languageSelect = document.getElementById('language-select');
434
+ const voiceSelect = document.getElementById('voice-select');
435
+ let voices = [];
436
+ let speech = new SpeechSynthesisUtterance();
437
+
438
+ submitButton.addEventListener('click', async () => {
439
+ if (toggle.checked) {
440
+ const prompt = promptInput.value;
441
+ const result = await performSearch(prompt);
442
+ chat(result);
443
+ speak(result);
444
+ } else {
445
+ chat(promptInput.value);
446
+ }
447
+ });
448
+
449
+ recording.addEventListener('click', async () => {
450
+ start();
451
+ });
452
+
453
+ stop.addEventListener('click', () => {
454
+ window.speechSynthesis.cancel();
455
+ document.getElementById("appleImage").style.display = 'none';
456
+ });
457
+
458
+ function sendRequest(json) {
459
+ const url = 'https://api.groq.com/openai/v1/chat/completions';
460
+ const options = {
461
+ method: 'POST',
462
+ headers: {
463
+ 'Authorization': 'Bearer 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',
464
+ 'Content-Type': 'application/json'
465
+ },
466
+ body: JSON.stringify({
467
+ messages: [{ role: 'user', content: json }],
468
+ model: 'llama3-70b-8192'
469
+ })
470
+ };
471
+
472
+ return fetch(url, options)
473
+ .then(response => response.json())
474
+ .then(data => {
475
+ const responseText = data.choices[0].message.content;
476
+ return responseText.replace(/\*/g, "");
477
+ })
478
+ .catch(error => console.error('Error:', error));
479
+ }
480
+
481
+ function performSearch(prompt) {
482
+ const searchTerm = prompt;
483
+ if (searchTerm.trim() === '') {
484
+ return ''; // Return empty string for empty search
485
+ }
486
+
487
+ const cx = '77f1602c0ff764edb';
488
+ const gcse = google.search.cse.element.getElement('searchresults-only0');
489
+ gcse.execute(searchTerm);
490
+
491
+ return new Promise((resolve) => {
492
+ setTimeout(() => {
493
+ const result = document.getElementById('___gcse_0').innerText;
494
+ const searchQuery = `${prompt} antworte kurz und knapp. antworte auf deutsch. du findest die antwort hier: ${result}`;
495
+ sendRequest(searchQuery).then(resolve);
496
+ }, 2000); // Adjust timeout as needed
497
+ });
498
+ }
499
+
500
+ function initSpeechRecognition() {
501
+ recognition = new webkitSpeechRecognition();
502
+ recognition.continuous = false;
503
+ recognition.interimResults = false;
504
+ recognition.lang = languageSelect.value || "de-DE";
505
+ }
506
+
507
+ function populateVoiceSelect(selectedLang) {
508
+ let langFilter = selectedLang || 'de-DE';
509
+ voices = speechSynthesis.getVoices().filter(voice => voice.lang === langFilter);
510
+ voiceSelect.innerHTML = '';
511
+ voices.forEach(voice => {
512
+ const option = document.createElement('option');
513
+ option.value = voice.name;
514
+ option.textContent = voice.name + ' (' + voice.lang + ')';
515
+ voiceSelect.appendChild(option);
516
+ });
517
+ if (voices.length > 0) {
518
+ voiceSelect.value = voices[0].name;
519
+ speech.voice = voices[0];
520
+ }
521
+ }
522
+
523
+ languageSelect.addEventListener('change', function() {
524
+ const selectedLang = languageSelect.value;
525
+ if (recognition) {
526
+ recognition.lang = selectedLang;
527
+ }
528
+ speech.lang = selectedLang;
529
+ populateVoiceSelect(selectedLang);
530
+ });
531
+
532
+ voiceSelect.addEventListener('change', function() {
533
+ const selectedVoice = voiceSelect.value;
534
+ speech.voice = voices.find(voice => voice.name === selectedVoice);
535
+ });
536
+
537
+ speechSynthesis.onvoiceschanged = function() {
538
+ populateVoiceSelect(languageSelect.value || 'de-DE');
539
+ };
540
+
541
+ // Initial population
542
+ populateVoiceSelect(languageSelect.value || 'de-DE');
543
+ initSpeechRecognition();
544
+
545
+ async function start() {
546
+ if (!recognition) {
547
+ initSpeechRecognition();
548
+ }
549
+
550
+ recognition.onresult = async function (event) {
551
+ for (let i = 0; i < event.results.length; i++) {
552
+ x = event.results[i][0].transcript;
553
+ if (toggle.checked) {
554
+ const result = await performSearch(x);
555
+ chat(result);
556
+ speak(result);
557
+ } else {
558
+ chat(x);
559
+ }
560
+ }
561
+ }
562
+ recognition.start();
563
+ }
564
+
565
+ let assist = '<div class="w3-cell-row" style="padding-left: 30px"><div class="w3-cell"><img src="mia.png"></img></div><div class="w3-cell"><h4>Ich bin ein hilfsbereiter KI Assistent. Wie kann ich ihnen helfen?</h4></div></div>';
566
+
567
+ function chat(response) {
568
+ const ul = document.getElementById("messages");
569
+ ul.innerHTML = ""; // Clear previous messages
570
+ const li = document.createElement("li");
571
+ const div = document.createElement("div");
572
+ div.innerHTML = "<md-block>" + response + "</md-block>";
573
+ li.appendChild(div);
574
+ ul.appendChild(li);
575
+ }
576
+
577
+ function speak(texte) {
578
+ speech.text = texte;
579
+ speech.lang = languageSelect.value || "de-DE";
580
+ window.speechSynthesis.speak(speech);
581
+ document.getElementById("appleImage").style.display = 'block';
582
+ speech.onend = function() {
583
+ document.getElementById("appleImage").style.display = 'none';
584
+ };
585
+ console.log(texte);
586
+ }
587
+ </script>
588
  </body>
589
  </html>
590
  """