<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Stefan Pietrusky on Medium]]></title>
        <description><![CDATA[Stories by Stefan Pietrusky on Medium]]></description>
        <link>https://medium.com/@stefanpietrusky?source=rss-700be654e078------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_XYa6lOVGzIsZ67doGI91w.png</url>
            <title>Stories by Stefan Pietrusky on Medium</title>
            <link>https://medium.com/@stefanpietrusky?source=rss-700be654e078------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:17:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@stefanpietrusky/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Cross-platform deploy: Python + Electron]]></title>
            <link>https://medium.com/pythoneers/cross-platform-deploy-python-electron-c6218ff8f811?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6218ff8f811</guid>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[electron]]></category>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Mon, 23 Jun 2025 11:43:51 GMT</pubDate>
            <atom:updated>2025-07-10T21:45:04.168Z</atom:updated>
            <content:encoded><![CDATA[<h4>Step-by-step to a ready-to-install desktop app with Flask server and NSIS</h4><p>This article shows how a Python code can be converted into an installable desktop app using Electron. Electron is an open-source framework that can be used to create cross-platform desktop apps based on HTML, CSS and JS. To achieve this, Chromium (display of the app’s user interface) and Node.js (execution of server-side JS code) are combined. This specific example shows how a Python script can be converted into an app for Windows. The big advantage is that the developed application can also be used by people with less technical experience. From my own experience, I know that even installing an application via GitHub is a hurdle for many people. An installable desktop app makes it much easier and more user-friendly to use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WK6gEI7JunfFrkJAGY-29Q.png" /><figcaption>GitHub is a hurdle (Image by author)</figcaption></figure><p>The process is explained using the FACTS V2 (tool for data analysis) [1] app as an example, the source code of which is available in the following <a href="https://github.com/stefanpietrusky/FACTSV2">repository</a> [2]. In order to use the app, <a href="https://ollama.com/">Ollama</a> [3] must be installed and a customized version of the LLM <a href="https://ollama.com/library/llama3.1:8b">llama3.1:8b</a> [4]. The model can be adapted using the <a href="https://github.com/stefanpietrusky/FACTSV2/blob/main/modelfile%5BP%5D.txt">modelfile</a> [5] in the repository. Alternatively, you can use your own code and model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8lgZQiQRFx1yU9FTvn-VYA.gif" /><figcaption>FACTS V2 prototype (Gif by author)</figcaption></figure><h4>ELECTRON AS WRAPPER FOR THE FLASK SERVER</h4><p>The app started via the Python script uses Flask as a local web server and this can be easily maintained using the method described here. Using the Node.js <em>child_process</em> module, the Python server (e.g. via python app.py) is automatically started when the Electron app is started. A <em>BrowserWindow</em> is then created in Electron that loads the URL (e.g. http://localhost:5000 or another configured port). The big advantage is that the existing Python code does not need to be adapted or rewritten. To ensure that the finished application can run on other systems, the Python code is bundled with <a href="https://pypi.org/project/pyinstaller/">PyInstaller</a> [6]. This means that no local Python installation is required by the end user. Likewise, no additional dependencies need to be installed manually.</p><h4>0 PREPARE SOURCE CODE</h4><p>The app must be installed in a location where there is write access without admin rights. Depending on the code, the app may attempt to create static files (HTML, CSS and JS), which is not permitted in most directories. If the app is installed in <em>“Program Files” </em>(e.g. via NSIS with Electron Builder), a normal user has no write permissions in this folder, as Windows protects installation directories by default. Therefore, if files are to be created or changed during runtime (e.g. HTML, logs, configuration files, etc.), these must be moved to user-specific or system-wide directories. Typical locations are <em>%LOCALAPPDATA%, %APPDATA%</em> and <em>%TEMP%</em>. Below is an example customization to ensure that the .html file is created in the writable directory.</p><pre>user_data_dir = Path(os.getenv(&#39;LOCALAPPDATA&#39;, read_only_path)) / &quot;MeineApp&quot;<br>user_data_dir.mkdir(parents=True, exist_ok=True)<br><br>def generiere_html():<br>    html_content = &quot;&quot;&quot;<br>    &lt;!DOCTYPE html&gt;<br>    &lt;html lang=&quot;de&quot;&gt;<br>    &lt;head&gt;<br>        &lt;meta charset=&quot;UTF-8&quot;&gt;<br>        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>        &lt;title&gt;FACTS&lt;/title&gt;<br>        &lt;link rel=&quot;stylesheet&quot; href=&quot;/styles.css&quot;&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        &lt;div class=&quot;container&quot;&gt;<br>            &lt;h1&gt;TEST APP&lt;/h1&gt;<br>            &lt;!-- ... Remaining HTML content ... --&gt;<br>        &lt;/div&gt;<br>        &lt;script src=&quot;/script.js&quot;&gt;&lt;/script&gt;<br>    &lt;/body&gt;<br>    &lt;/html&gt;<br>    &quot;&quot;&quot;<br>    with open(user_data_dir / &quot;index.html&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<br>        file.write(html_content)<br>@app.route(&quot;/&quot;)<br>def serve_index():<br>    return send_from_directory(user_data_dir, &quot;index.html&quot;)<br><br>if __name__ == &quot;__main__&quot;:<br>    generiere_html()</pre><p>With the code shown above, the static file <em>“index.html”</em> is recreated in the writable folder each time it is started and then read from there. This avoids permission errors that can occur when an application attempts to create or change files in write-protected directories. The adjustment is then also made for the other static files. If you do not want to recreate the files each time, you can also check once at startup whether the file already exists and only create it if it does not exist.</p><h4>1 INSTALL PREREQUISITES</h4><p>If Node.js is not yet installed, it should be downloaded and set up now. The current version is available on the <a href="https://nodejs.org/">official website</a> [7]. A new Electron project is then set up in the directory of the Python script. To do this, open a terminal (or a command prompt or PowerShell) and execute the following command:</p><pre>npm init -y <br>npm install electron --save-dev</pre><p>The first command creates the <em>“package.json”</em> file in which the start script is later customized. The second creates the “node_modules” directory and the <em>“package-look.json”</em> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HxCYEEU9Sl6Z9hYMKSSDiQ.png" /><figcaption>The default package.json file (Image by author)</figcaption></figure><p>The core modules (e.g. fs, path, http, os etc.) are located in <em>“node_modules”</em>. Depending on the project, external modules must be implemented in the directory.</p><h4>2 CREATE PROJECT STRUCTURE</h4><p>The file <em>“main.js”</em> is now created in the project directory. This file is responsible for the Electron main process, which starts the Flask server and opens the browser window with the user interface. After this step, the following files and folders should be present in the project directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ksahT87cRE5KquLJun4lEw.png" /><figcaption>Overview of current files and directories (Image by author)</figcaption></figure><h4>3 START FLASK SERVER WITH ELECTRON</h4><p>Insert the following code into the <em>“main.js”</em> file:</p><pre>const { app, Menu, BrowserWindow } = require(&#39;electron&#39;);<br>const { spawn } = require(&#39;child_process&#39;);<br>const path = require(&#39;path&#39;);<br>const fs = require(&#39;fs&#39;);<br>const http = require(&#39;http&#39;);<br><br>let pyProc = null;<br>let mainWindow = null;<br>let firstRun = true;<br><br>app.on(&#39;browser-window-created&#39;, () =&gt; {<br>  Menu.setApplicationMenu(null);<br>});<br><br>function createWindow() {<br>  mainWindow = new BrowserWindow({<br>    width: 1280,<br>    height: 800,<br>    webPreferences: {<br>      nodeIntegration: false,<br>      devTools: false<br>    }<br>  });<br><br>  Menu.setApplicationMenu(null);<br><br>  mainWindow.loadURL(&#39;http://127.0.0.1:5000&#39;);<br><br>  mainWindow.webContents.once(&#39;did-finish-load&#39;, () =&gt; {<br>    if (firstRun) {<br>      firstRun = false;<br>      mainWindow.webContents.reloadIgnoringCache();<br>    }<br>  });<br><br>  mainWindow.on(&#39;closed&#39;, () =&gt; {<br>    mainWindow = null;<br>  });<br>}<br><br>function startFlaskServer() {<br>  const exePath = path.join(process.resourcesPath, &#39;app.exe&#39;);<br><br>  if (!fs.existsSync(exePath)) {<br>    console.error(&#39;app.exe not found in:&#39;, exePath);<br>    return;<br>  }<br><br>  pyProc = spawn(exePath, []);<br><br>  pyProc.stdout.on(&#39;data&#39;, (data) =&gt; {<br>    console.log(`Flask stdout: ${data}`);<br>  });<br>  pyProc.stderr.on(&#39;data&#39;, (data) =&gt; {<br>    console.error(`Flask stderr: ${data}`);<br>  });<br>  pyProc.on(&#39;close&#39;, (code) =&gt; {<br>    console.log(`Flask server exited with code ${code}`);<br>  });<br>}<br><br>function waitForServer(callback, retryDelay = 500, maxRetries = 20) {<br>  const options = {<br>    host: &#39;127.0.0.1&#39;, <br>    port: 5000,<br>    timeout: 1000<br>  };<br><br>  const req = http.get(options, (res) =&gt; {<br>    console.log(`Server ist erreichbar: ${res.statusCode}`);<br>    callback();<br>  });<br><br>  req.on(&#39;error&#39;, (err) =&gt; {<br>    console.log(&#39;Server not yet available, try again...&#39;);<br>    if (maxRetries &gt; 0) {<br>      setTimeout(() =&gt; {<br>        waitForServer(callback, retryDelay, maxRetries - 1);<br>      }, retryDelay);<br>    } else {<br>      console.error(&#39;Server could not be reached after several attempts.&#39;);<br>    }<br>  });<br><br>  req.on(&#39;timeout&#39;, () =&gt; {<br>    req.abort();<br>  });<br>}<br><br>app.on(&#39;ready&#39;, () =&gt; {<br>  startFlaskServer();<br>  waitForServer(createWindow);<br>});<br><br>app.on(&#39;window-all-closed&#39;, () =&gt; {<br>  if (pyProc) {<br>    pyProc.kill();<br>  }<br>  if (process.platform !== &#39;darwin&#39;) {<br>    app.quit();<br>  }<br>});<br><br>app.on(&#39;browser-window-created&#39;, () =&gt; {<br>  if (process.platform === &#39;darwin&#39;) {<br>    Menu.setApplicationMenu(Menu.buildFromTemplate([]));<br>  } else {<br>    Menu.setApplicationMenu(null);<br>  }<br>});<br><br>app.on(&#39;activate&#39;, () =&gt; {<br>  if (mainWindow === null) {<br>    createWindow();<br>  }<br>});</pre><p>The code initializes an Electron app, which first starts a Flask application integrated into the app (as app.exe in the resource directory) via <em>child_process.spawn</em> and writes its output to the log. An HTTP request is then sent at regular intervals (maximum 20 attempts at 500 ms intervals) to check whether the local server on port 5000 can already be reached; as soon as it responds, a new browser window (<em>BrowserWindow</em>) is opened, which points to <em>http://127.0.0.1:5000</em>. The window is forcibly reloaded once the first time it is loaded in order to avoid cache problems. In all cases, the standard menu is removed via the menu module (under macOS, it may be replaced by an empty menu template). When all windows are closed, the Flask process instance is terminated and the app is closed on non-macOS systems. In addition, the activate event ensures that a new window is opened under macOS when clicking on the dock icon if all windows were previously closed.</p><h4>4 CUSTOMIZE START SCRIPT</h4><p>The <em>“package.json” </em>file is adapted as follows:</p><pre>{<br>  &quot;name&quot;: &quot;test&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;main&quot;: &quot;main.js&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;keywords&quot;: [],<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;email&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;&quot;,<br>  &quot;devDependencies&quot;: {<br>    &quot;electron&quot;: &quot;^35.0.1&quot;,<br>    &quot;electron-builder&quot;: &quot;^23.6.0&quot;,<br>    &quot;electron-packager&quot;: &quot;^17.1.2&quot;<br>  },<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;electron .&quot;,<br>    &quot;pack&quot;: &quot;electron-builder --dir&quot;,<br>    &quot;dist&quot;: &quot;electron-builder&quot;<br>  },<br>  &quot;build&quot;: {<br>    &quot;appId&quot;: &quot;test.id&quot;,<br>    &quot;productName&quot;: &quot;test&quot;,<br>    &quot;win&quot;: {<br>      &quot;target&quot;: &quot;nsis&quot;,<br>      &quot;icon&quot;: &quot;&quot;<br>    },<br>    &quot;nsis&quot;: {<br>      &quot;include&quot;: &quot;&quot;,<br>      &quot;oneClick&quot;: false,<br>      &quot;perMachine&quot;: true,<br>      &quot;allowElevation&quot;: true,<br>      &quot;installerIcon&quot;: &quot;&quot;,<br>      &quot;license&quot;: &quot;&quot;,<br>      &quot;allowToChangeInstallationDirectory&quot;: true<br>    },<br>    &quot;files&quot;: [<br>      &quot;main.js&quot;,<br>      &quot;package.json&quot;<br>    ],<br>    &quot;extraResources&quot;: [<br>      {<br>        &quot;from&quot;: &quot;dist/app/&quot;, <br>        &quot;to&quot;: &quot;app&quot;, <br>        &quot;filter&quot;: [&quot;**/*&quot;]<br>      }<br>    ]<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;ip&quot;: &quot;^2.0.1&quot;<br>  }<br>}</pre><p>The file begins with general metadata (name, version, author, keywords etc.). This is followed by the entry point. The <em>“main” </em>defines which file is loaded when Electron is started. The runtime dependencies are executed under <em>“dependencies”</em>. There are <em>“devDependencies”</em> for development and packing. The short commands that can be called with <em>npm run &lt;name&gt;</em> are listed in <em>“scripts”</em>. If you want to start the app in developer mode, select <em>“start”</em>. The complete installer package is created with <em>“dist”</em>, without installer <em>“pack”</em>. The build configuration is defined with <em>“build”</em>. If files (e.g. graphics etc.) or folders are to be copied during installation, these are defined under <em>“extraResources”</em>.</p><h4>5 PREPARE DISTRIBUTION</h4><p>As mentioned at the beginning, the application should be provided in such a way that it can also be used by less technically experienced users without any problems. For this reason, the code is compiled so that the target users do not need their own Python installation. Pyinstaller is used for implementation. The installation is carried out using the following command:</p><pre>pip install pyinstaller</pre><p>Pyinstaller creates an executable file<em> “app.exe”</em> from our Python code <em>“app.py”</em>. The <em>“app.exe”</em> file is stored in the newly created <em>“dist”</em> directory, more precisely in a subfolder with the name of the project. To obtain the <em>“app.exe”</em> file, the following command is executed:</p><pre>pyinstaller app.py</pre><p>For a professional distribution, <em>“electron-builder” </em>should be used. This creates an installer that installs the app and includes all dependencies. The builder is installed using the following command.</p><pre>npm install electron-builder --save-dev</pre><p>Next, we execute the <em>“dist”</em> script defined in the “<em>scripts” </em>key of the <em>“package.json”</em> file. In our case, this script calls the previously installed <em>“electron-builder”</em> and creates an installation package for the app. The command is as follows:</p><pre>npm run dist</pre><p>The command starts the CLI tool <em>“electron-builder”</em>, which packs the <em>“app.exe”</em> into an installation package. By default, the <em>“build”</em> key is read out. Depending on the configuration, packing then takes place, followed by building. In the example file <em>“package.json”</em>, only Windows <em>“win”</em> is defined as the target platform. However, macOS or Linux can also be considered as a platform.</p><pre>  &quot;win&quot;: {<br>    &quot;target&quot;: [&quot;nsis&quot;, &quot;portable&quot;]<br>  },<br>  &quot;mac&quot;: {<br>    &quot;target&quot;: [&quot;dmg&quot;]<br>  },<br>  &quot;linux&quot;: {<br>    &quot;target&quot;: [&quot;AppImage&quot;]<br>  }</pre><p>As the command creates an installer, an error message may appear. To resolve this, the development mode may need to be activated in the system (developer settings). Admin rights are required for this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cpO5Ux4Da1OPA0RPwCIA_w.png" /><figcaption>The developer mode must be activated [here in german] (Image by author)</figcaption></figure><p>New files and directories have been created in the <em>“dist”</em> directory. The unpacked version of the app is located in the <em>“win-unpacked”</em> directory. The folder contains the unpacked app. This folder contains the running version of the app before it is packed into an .exe file. If the app does not start when double-clicked, you should run it via the terminal in this directory to see any error messages directly. Depending on the configuration, the file for the actual installation ends with <em>…Setup 1.0.0.exe</em>. The <em>“resources” </em>directory contains the files <em>“app.exe” </em>and <em>“app.asar”</em>. The latter contains the entire Electron code (HTML, CSS and JS). Both files are necessary for the application to function. If everything is set up correctly, the application that was previously started with “<em>python app.py”</em> can now be started via an icon, like a desktop app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BkPc8Uzc80ebOk1ELoOc1w.gif" /><figcaption>From Python to app ready (Gif by author)</figcaption></figure><h4>6 CUSTOMIZE INSTALLATION</h4><p>The installer can now be made available to other users so that they can install the app on their system simply and easily. The <em>“package.json”</em> file can be used to make adjustments to the installation process to make it more professional. For example, a custom icon can be defined in the <em>“build”</em> key. The icon must be in .ico format. There are various conversion tools on the Internet that convert .png files accordingly. A simple conversion is made possible by the <a href="https://imagemagick.org/index.php">ImageMagick</a> [8] tool. After installation, execute the following command:</p><pre>magick icon.png -define icon:auto-resize=256,128,64,48,32,16 icon.ico</pre><p>Keywords can be defined for the app, as well as a general description and a license .md file, which must be confirmed by the user during installation. The correct character encoding must be ensured here. The license file should be saved as UTF-8 with BOM (Byte Order Mark) so that the special characters are displayed correctly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DIz3xVwSm5B6QTjsXwDbdQ.png" /><figcaption>UTF-8 with BOM in VSC (Image by author)</figcaption></figure><p>The icons for the installer and uninstaller can also be defined. In the current <em>“package.json”</em> file, many of these keys are not defined or have no value.</p><h4>7 INSTALLING EXTERNAL TOOLS [NSIS INSTALLER]</h4><p>To further simplify the use of the app for end users, external tools can be automatically implemented in the installation process using the NSIS Installer (Nullsoft Scriptable Install System). In order to use the FACTS app, for example, Ollama must be installed on the system. The installation of Ollama (pull instead of run) and the appropriate LLM can be integrated into the installation process. Specifically, an <em>“installer.nsh”</em> file is created and integrated via <em>“package.json”</em> using the Electron Builder during the installation.</p><pre>!macro customInstall<br>  ; 1) Copy the Ollama installer to $INSTDIR and execute it<br>  File &quot;\ollama_installer.exe&quot;<br><br>  ; /S, /silent, /quiet etc. - depends on the Ollama EXE.<br>  ExecWait &#39;&quot;$INSTDIR\ollama_installer.exe&quot; /quiet&#39; $1<br>  ${If} $1 &lt;&gt; 0<br>    MessageBox MB_OK &quot;Ollama installation failed (Exitcode $1).&quot;<br>    Abort<br>  ${EndIf}<br><br>  ; 2) Copy modelfile.txt to $INSTDIR<br>  File &quot;\modelfile.txt&quot;<br>  ; =&gt; is now located under $INSTDIR\modelfile.txt<br><br>  ; 3) Determine LOCALAPPDATA via System::Call<br>  System::Call &#39;kernel32::GetEnvironmentVariableA(&quot;LOCALAPPDATA&quot;, r9, 1024) i.r1&#39;<br><br>  ; 4) Download model &quot;llama3.1&quot;<br>  ExecWait &#39;cmd /c &quot;ollama pull llama3.1&quot;&#39; $2<br>  ${If} $2 &lt;&gt; 0<br>    MessageBox MB_OK &quot;Download of model llama3.1 failed (Exitcode $2).&quot;<br>    Abort<br>  ${EndIf}<br><br>  ; 5) Create user-defined model (e.g. &quot;llama3.1p&quot;)<br>  ExecWait &#39;cmd /c &quot;ollama create llama3.1p -f &quot;$INSTDIR\modelfile.txt&quot;&#39; $3<br>  ${If} $3 &lt;&gt; 0<br>    MessageBox MB_OK &quot;Failed to create the custom model (Exitcode $3).&quot;<br>    Abort<br>  ${EndIf}<br><br>!macroend</pre><p>The .nsh file shown also installs Ollama and an LLM during the installation of the app. There is no silent parameter for the Ollama installer yet, which allows you to hide the installation window. The file is referenced in the <em>“nsis”</em> key under the <em>“include” </em>attribute in the <em>“package.json”</em> file.<br>This means that it is included and executed when the installer is created by the Electron Builder.</p><pre>&quot;nsis&quot;: {<br>  &quot;include&quot;: &quot;build/installer.nsh&quot;<br>}</pre><h4>8 MAXIMUM RECURSION DEPTH</h4><p>If you compile an extensive app with PyInstaller, the error message <em>“RecursionError”</em> may occur. The error occurs if too many module imports are run through recursively. With heavily nested dependencies, this can lead to the maximum recursion depth being exceeded. Pyton limits this to 1.000. To solve the problem, open the. spec file, which is automatically generated during the first compilation process. Alternatively, you can create the file manually with the following command:</p><pre>pyi-makespec app.py</pre><p>The following line (directly below the comment) is added to the .spec file, which increases the maximum recursion depth from 1.000 to 5.000.</p><pre># -*- mode: python ; coding: utf-8 -*-<br><br>import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)<br><br>a = Analysis(<br>    [&#39;app.py&#39;],<br>    pathex=[],<br>    binaries=[],<br>    datas=[],<br>    hiddenimports=[],<br>    hookspath=[],<br>    hooksconfig={},<br>    runtime_hooks=[],<br>    excludes=[],<br>    noarchive=False,<br>    optimize=0,<br>)<br>pyz = PYZ(a.pure)<br><br>exe = EXE(<br>    pyz,<br>    a.scripts,<br>    a.binaries,<br>    a.datas,<br>    [],<br>    name=&#39;app&#39;,<br>    debug=False,<br>    bootloader_ignore_signals=False,<br>    strip=False,<br>    upx=True,<br>    upx_exclude=[],<br>    runtime_tmpdir=None,<br>    console=True,<br>    disable_windowed_traceback=False,<br>    argv_emulation=False,<br>    target_arch=None,<br>    codesign_identity=None,<br>    entitlements_file=None,<br>)</pre><p>After that, you no longer compile with <em>“app.py”</em>, but with <em>“app.spec”:</em></p><pre>pyinstaller app.spec </pre><p>If you are not working in a virtual environment and run PyInstaller in the global Python installation, all dependencies are collected, even if they are not imported into the project. As this can take a very long time depending on the system, the process will most likely simply be terminated by cmd.exe without an error message. To prevent this, you must list all larger packages in the<em> “app.spec”</em> file under<em> “excludes=[]”</em> to shorten the process.</p><h4>9 FURTHER INFORMATION</h4><p>There are various ways to protect the app code. One of these is to deactivate the developer tools. This is already set up by default in the <em>“main.js”</em> file. The standard menus of the Electron app are also deactivated. It can be implemented as follows:</p><pre>devTools: false</pre><p>To prevent the app from being flagged as unknown by virus programmes, a certificate is required, which can be purchased <a href="https://www.digicert.com/">here</a> [9], among other places.</p><h4>CONCLUSSION</h4><p>This post showed how to compile a Python script using PyInstaller and turn it into an Electron app that others can easily install on their system without having to deal with complicated dependencies, repositories or the like. Depending on the system used, the package.json file can be customised. It was also explained how the installation process can be customised and extended using NSIS.</p><p>[1] Pietrusky, S. [2025]. How I automatically find numerous answers for any given question. FACTS V2: Filtering and Analysis of Content in Textual Sources. Data Science Collective. <a href="https://medium.com/data-science/facts-v2-filtering-and-analysis-of-content-in-textual-sources-1a16cdac811b">Link</a></p><p>[2] <a href="https://github.com/stefanpietrusky/FACTSV2">https://github.com/stefanpietrusky/FACTSV2</a></p><p>[3] <a href="https://ollama.com/">https://ollama.com/</a></p><p>[4] <a href="https://ollama.com/library/llama3.1:8b">https://ollama.com/library/llama3.1:8b</a></p><p>[5] <a href="https://github.com/stefanpietrusky/FACTSV2/blob/main/modelfile%5BP%5D.txt">https://github.com/stefanpietrusky/FACTSV2/blob/main/modelfile%5BP%5D.txt</a></p><p>[6] <a href="https://pypi.org/project/pyinstaller/">https://pypi.org/project/pyinstaller/</a></p><p>[7] <a href="https://nodejs.org/">https://nodejs.org</a></p><p>[8] <a href="https://imagemagick.org/index.php">https://imagemagick.org/index.php</a></p><p>[9] <a href="https://www.digicert.com/">https://www.digicert.com/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6218ff8f811" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pythoneers/cross-platform-deploy-python-electron-c6218ff8f811">Cross-platform deploy: Python + Electron</a> was originally published in <a href="https://medium.com/pythoneers">The Pythoneers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I automatically find numerous answers for any given question.]]></title>
            <link>https://medium.com/data-science-collective/facts-v2-filtering-and-analysis-of-content-in-textual-sources-1a16cdac811b?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a16cdac811b</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Fri, 04 Apr 2025 13:22:41 GMT</pubDate>
            <atom:updated>2025-05-22T20:14:08.833Z</atom:updated>
            <content:encoded><![CDATA[<h4>FACTS V2: Filtering and Analysis of Content in Textual Sources</h4><p>This article discusses the self-developed application FACTS (Filtering and Analysis of Content in Textual Sources), which is divided into three main areas and automates the analysis and evaluation of large volumes of text. The first area deals with data acquisition. By using web scraping (Selenium and BeautifulSoup) and direct API queries, scientific papers are downloaded from various databases (ERIC, peDOCs [only german papers] and arXiv), depending on the search parameters (topic and year of publication), and stored according to APA citation in order to be able to reference them more easily. The data or publications do not have to come from the available databases; you can also analyze your own data collections. The second part is about data analysis. The publications are converted into text, cleansed and searched for previously defined questions using a locally installed LLM in order to generate concise answers. The last part is about data analysis. Here, topic modelling is carried out using the LDA (Latent Dirichlet Allocation) algorithm and the results are visualized using word clouds and interactive knowledge graphs. The identified topics are also automatically interpreted by an LLM and linked to each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pf4Ntl3-UXdoJ5FaLbvgQg.png" /><figcaption>Components of the FACTS app (<em>Image by author)</em></figcaption></figure><p>The application has already been tested several times [1], most recently at the 6th IGSP Congress, which took place at the PH Heidelberg and dealt with professionalization through school practice. Here, FACTS V1.5 was tested on the basis of the question in topic area 1. FACTS was able to provide answers to the questions posed at the congress by examining 167 papers from the year 2024. The results, which are yet to be published, show that the application can provide significant added value in answering scientific questions, including in terms of time savings, depending on the system available.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*txKz14avpYN-ML7NsQawQA.png" /><figcaption>Testing of the FACTS app at the 6th IGSP Congress, study in numbers (<em>Image by author)</em></figcaption></figure><p>The app was already being developed further before the congress. Now the version is being released that uses an interactive interface to execute the three components described using a common script. Previously, the components had to be executed sequentially via three scripts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V5RZIfUATH_oEDW9_tRK2A.png" /><figcaption>How FACTS V1.5 works (<em>Image by author)</em></figcaption></figure><p>Flask is used to implement a web server that provides various endpoints. The user can adjust the app parameters such as database, search terms, publication year and PDF directories via the front end, which was implemented using HTML, CSS and JS. The progress of the three components is reported back asynchronously via JSON APIs. The global variable <em>“current_process”</em> was integrated in combination with a thread lock <em>“process_lock”</em> to prevent several computationally intensive processes from starting at the same time. The structure of the app is shown below. The code is available via a GitHub repository. The technical functionality of the individual areas is then described.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8lgZQiQRFx1yU9FTvn-VYA.gif" /><figcaption>Structure of FACTS V2 (<em>GIF by author)</em></figcaption></figure><h4>DATA COLLECTION (Download of publications)</h4><p>This section of FACTS deals with the retrieval of scientific publications from various databases. To download the publications from the ERIC database <em>“download_pdf_eric”</em>, the required URL is assembled with the specific paper ID. The <em>“requests”</em> library is used to download the file and save it locally. The metadata (car, year, title, URL) is then used to create an APA-compliant citation using the <em>“generate_apa_citation”</em> function. The relevant information (e.g. paper ID) is extracted from the HTML elements (title element) using the <em>“extract_paper_data_eric”</em> function and <em>“BeautifulSoup”</em>.</p><pre>def download_pdf_eric(paper_id, download_folder, session=None, file_name=None):<br>    pdf_url = f&quot;{PDF_BASE_URL}{paper_id}.pdf&quot;<br>    pdf_filename = os.path.join(download_folder, file_name) if file_name else os.path.join(download_folder, f&quot;{paper_id}.pdf&quot;)<br>    session = session or requests.Session()<br>    <br>    headers = {<br>        &quot;User-Agent&quot;: (&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) &quot;<br>                       &quot;AppleWebKit/537.36 (KHTML, like Gecko) &quot;<br>                       &quot;Chrome/85.0.4183.121 Safari/537.36&quot;)<br>    }<br>    <br>    try:<br>        logging.info(f&quot;Download PDF: {pdf_url}&quot;)<br>        response = session.get(pdf_url, timeout=30, headers=headers)<br>        response.raise_for_status()  <br>        with open(pdf_filename, &quot;wb&quot;) as f:<br>            f.write(response.content)<br>        logging.info(f&quot;PDF saved: {pdf_filename}&quot;)<br>        return pdf_url<br>    except Exception as e:<br>        logging.error(f&quot;PDF Download Error: {e}&quot;)<br>        return None<br><br>def extract_paper_data_eric(paper_div, base_url):<br>    paper_id = paper_div.get(&quot;id&quot;, &quot;&quot;)<br>    paper_id = re.sub(r&quot;^r_&quot;, &quot;&quot;, paper_id)<br>    if not paper_id:<br>        logging.warning(&quot;No valid paper ID found, skip.&quot;)<br>        return None<br><br>    title_elem = paper_div.find(&#39;div&#39;, class_=&#39;r_t&#39;).find(&#39;a&#39;)<br>    title = title_elem.text.strip() if title_elem else &quot;Unknown title&quot;<br>    paper_url = base_url + title_elem[&#39;href&#39;] if title_elem else None<br><br>    metadata_elem = paper_div.find(&#39;div&#39;, class_=&#39;r_a&#39;)<br>    metadata_text = metadata_elem.text.strip() if metadata_elem else &quot;Unknown source&quot;<br>    if &#39;,&#39; in metadata_text:<br>        author_journal, pub_year = metadata_text.rsplit(&#39;,&#39;, 1)<br>        pub_year = pub_year.strip()<br>    else:<br>        author_journal, pub_year = metadata_text, &quot;o.J.&quot;<br><br>    return {<br>        &quot;paper_id&quot;: paper_id,<br>        &quot;title&quot;: title,<br>        &quot;paper_url&quot;: paper_url,<br>        &quot;author_journal&quot;: author_journal,<br>        &quot;pub_year&quot;: pub_year<br>    }<br><br>def generate_apa_citation(metadata, database):<br>    authors = metadata.get(&quot;authors&quot;, [])<br>    if isinstance(authors, list):<br>        author_str = &quot;, &quot;.join(authors) if authors else &quot;Unknown author&quot;<br>    else:<br>        author_str = authors or &quot;Unknown author&quot;<br>    year = metadata.get(&quot;year&quot;, &quot;o.J.&quot;)<br>    title = metadata.get(&quot;title&quot;, &quot;No title&quot;)<br>    citation = f&quot;{author_str} ({year}). {title}. {database}. Retrieved from {metadata.get(&#39;url&#39;, &#39;n/a&#39;)}&quot;<br>    return citation</pre><p>As some databases do not have an API, a Selenium-based search and download option has been set up for ERIC and peDOCs. The functions <em>“download_eric_selenium”</em> and <em>“download_pedocs”</em> start a Firefox browser in headless mode using a web driver, in this case Geckodriver. THE PATH TO THE WEBDRIVER (GECKO_DRIVER_PATH) MUST BE ADAPTED SO THAT THIS PART OF THE APP WORKS [2]. Depending on your preferred Internet browser, you can of course use a different WebDriver. Navigate to the respective website (e.g. pedocs.de or eric.ed.gov), enter the search term and filter accordingly (e.g. peer review and full text). The functions check whether the publication year of the publication matches the target year entered in the interface. If this is the case, the publication is downloaded and the metadata is saved in a .txt file to save the source information.</p><pre>def download_eric_selenium(query, target_year, num_papers, download_folder, progress_data):<br>    options = Options()<br>    options.headless = True<br>    options.add_argument(&#39;--headless&#39;)<br>    options.set_preference(&quot;permissions.default.image&quot;, 2)<br>    service = Service(executable_path=GECKO_DRIVER_PATH)<br>    driver = webdriver.Firefox(service=service, options=options)<br>    session = requests.Session()<br>    <br>    try:<br>        driver.get(&quot;https://eric.ed.gov/&quot;)<br>        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, &quot;s&quot;)))<br>        <br>        if progress_data.get(&quot;abort&quot;):<br>            progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>            driver.quit()<br>            return<br><br>        try:<br>            cookie_button = WebDriverWait(driver, 5).until(<br>                EC.element_to_be_clickable((By.XPATH, &quot;//button[contains(text(), &#39;Accept&#39;)]&quot;))<br>            )<br>            cookie_button.click()<br>        except TimeoutException:<br>            logging.info(&quot;No cookie pop-up found.&quot;)<br><br>        search_box = driver.find_element(By.ID, &quot;s&quot;)<br>        search_box.clear()<br>        search_box.send_keys(query)<br>        <br>        for name, desc in [(&quot;pr&quot;, &quot;Peer-Review&quot;), (&quot;ft&quot;, &quot;Full text available&quot;)]:<br>            if progress_data.get(&quot;abort&quot;):<br>                progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>                driver.quit()<br>                return<br>            try:<br>                checkbox = driver.find_element(By.NAME, name)<br>                if not checkbox.is_selected():<br>                    checkbox.click()<br>            except NoSuchElementException:<br>                logging.warning(f&quot; {desc} Checkbox not found!&quot;)<br>        <br>        search_box.send_keys(Keys.RETURN)<br>        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, &quot;r_i&quot;)))<br>        <br>        downloaded_papers = 0<br>        os.makedirs(download_folder, exist_ok=True)<br>        references_file = os.path.join(download_folder, &quot;sources.txt&quot;)<br>        <br>        base_url = &quot;https://eric.ed.gov/&quot;<br>        eric_index = 1  <br>        <br>        with open(references_file, &#39;a&#39;, encoding=&#39;utf-8&#39;) as ref_file:<br>            while downloaded_papers &lt; num_papers:<br>                if progress_data.get(&quot;abort&quot;):<br>                    progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>                    driver.quit()<br>                    return<br><br>                soup = BeautifulSoup(driver.page_source, &#39;html.parser&#39;)<br>                paper_divs = soup.find_all(&#39;div&#39;, class_=&#39;r_i&#39;)<br>                if not paper_divs:<br>                    logging.error(&quot;No further papers found!&quot;)<br>                    break<br><br>                for paper in paper_divs:<br>                    if progress_data.get(&quot;abort&quot;):<br>                        progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>                        driver.quit()<br>                        return<br><br>                    if downloaded_papers &gt;= num_papers:<br>                        break<br><br>                    data = extract_paper_data_eric(paper, base_url)<br>                    if not data:<br>                        continue<br>                    if data[&quot;pub_year&quot;] != str(target_year):<br>                        logging.info(f&quot; {data[&#39;title&#39;]} is skipped (Year {data[&#39;pub_year&#39;]}, expected: {target_year})&quot;)<br>                        continue<br><br>                    pdf_url = download_pdf_eric(<br>                        data[&quot;paper_id&quot;],<br>                        download_folder,<br>                        session,<br>                        file_name=f&quot;eric_{eric_index}.pdf&quot;<br>                    )<br>                    metadata = {<br>                        &quot;title&quot;: data[&quot;title&quot;],<br>                        &quot;authors&quot;: data[&quot;author_journal&quot;],<br>                        &quot;year&quot;: data[&quot;pub_year&quot;],<br>                        &quot;url&quot;: pdf_url<br>                    }<br>                    citation = generate_apa_citation(metadata, &quot;ERIC&quot;)<br>                    ref_file.write(f&quot;{eric_index}. {citation}\n\n&quot;)<br>                    <br>                    downloaded_papers += 1<br>                    progress_data[&quot;completed&quot;] = downloaded_papers<br>                    progress_data[&quot;percent&quot;] = int((downloaded_papers / num_papers) * 100)<br>                    eric_index += 1<br><br>                if progress_data.get(&quot;abort&quot;):<br>                    progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>                    driver.quit()<br>                    return<br><br>                try:<br>                    next_page_link = WebDriverWait(driver, 10).until(<br>                        EC.element_to_be_clickable((By.XPATH, &quot;//a[contains(text(), &#39;Next Page&#39;)]&quot;))<br>                    )<br>                    next_page_link.click()<br>                    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, &quot;r_i&quot;)))<br>                except TimeoutException:<br>                    logging.error(&quot;No further page found.&quot;)<br>                    break<br><br>        logging.info(&quot;ERIC-Scraping completed.&quot;)<br>        progress_data[&quot;status&quot;] = &quot;completed&quot;<br>    <br>    except Exception as e:<br>        progress_data[&quot;status&quot;] = &quot;error&quot;<br>        progress_data[&quot;error&quot;] = str(e)<br>    finally:<br>        driver.quit()</pre><p>For the arXiv database, the officially available API was used and implemented in FACTS. The <em>“download_arxiv”</em> function constructs an API query to the database, parses the XML result, and downloads the corresponding PDF file.</p><pre>def download_arxiv(query, year, num_papers, download_folder, progress_data):<br>    base_url = &quot;http://export.arxiv.org/api/query?&quot;<br>    search_query = f&quot;all:{query} AND submittedDate:[{year}01010000 TO {year}12312359]&quot;<br>    params = {<br>        &quot;search_query&quot;: search_query,<br>        &quot;start&quot;: 0,<br>        &quot;max_results&quot;: num_papers,<br>        &quot;sortBy&quot;: &quot;submittedDate&quot;,<br>        &quot;sortOrder&quot;: &quot;descending&quot;<br>    }<br>    query_url = base_url + &quot;&amp;&quot;.join([f&quot;{key}={value}&quot; for key, value in params.items()])<br>    <br>    if progress_data.get(&quot;abort&quot;):<br>        progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>        return<br><br>    try:<br>        response = requests.get(query_url, timeout=30)<br>        response.raise_for_status()<br>        root = ET.fromstring(response.content)<br>        ns = {&quot;atom&quot;: &quot;http://www.w3.org/2005/Atom&quot;}<br>        entries = root.findall(&quot;atom:entry&quot;, ns)<br><br>        total = min(num_papers, len(entries))<br>        progress_data[&quot;total&quot;] = total<br>        progress_data[&quot;completed&quot;] = 0<br>        citations = []<br>        paper_index = 1<br><br>        if progress_data.get(&quot;abort&quot;):<br>            progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>            return<br><br>        for idx, entry in enumerate(entries[:total], start=1):<br>            if progress_data.get(&quot;abort&quot;):<br>                progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>                return<br><br>            title_elem = entry.find(&quot;atom:title&quot;, ns)<br>            title = title_elem.text.strip() if title_elem is not None else &quot;No titel&quot;<br>            published_elem = entry.find(&quot;atom:published&quot;, ns)<br>            published = published_elem.text.strip() if published_elem is not None else &quot;0000-00-00T00:00:00Z&quot;<br>            published_year = published[:4]<br>            authors = [author.find(&quot;atom:name&quot;, ns).text for author in entry.findall(&quot;atom:author&quot;, ns)]<br>            <br>            pdf_url = None<br>            for link in entry.findall(&quot;atom:link&quot;, ns):<br>                if link.attrib.get(&quot;type&quot;) == &quot;application/pdf&quot;:<br>                    pdf_url = link.attrib.get(&quot;href&quot;)<br>                    break<br><br>            if pdf_url:<br>                pdf_filename = os.path.join(download_folder, f&quot;arxiv_{paper_index}.pdf&quot;)<br>                try:<br>                    r = requests.get(pdf_url, stream=True, timeout=30)<br>                    r.raise_for_status()<br>                    with open(pdf_filename, &quot;wb&quot;) as f:<br>                        for chunk in r.iter_content(chunk_size=8192):<br>                            f.write(chunk)<br>                except Exception as e:<br>                    logging.error(f&quot;PDF Download Error: {e}&quot;)<br><br>            metadata = {<br>                &quot;title&quot;: title,<br>                &quot;authors&quot;: authors,<br>                &quot;year&quot;: published_year,<br>                &quot;url&quot;: pdf_url if pdf_url else entry.find(&quot;atom:id&quot;, ns).text<br>            }<br>            citation = generate_apa_citation(metadata, &quot;arxiv&quot;)<br>            citations.append(citation)<br><br>            progress_data[&quot;completed&quot;] = idx<br>            progress_data[&quot;percent&quot;] = int((idx / total) * 100)<br>            paper_index += 1<br><br>        progress_data[&quot;status&quot;] = &quot;completed&quot;<br><br>        if progress_data.get(&quot;abort&quot;):<br>            progress_data[&quot;status&quot;] = &quot;aborted&quot;<br>            return<br><br>        citations_file = os.path.join(download_folder, &quot;sources.txt&quot;)<br>        with open(citations_file, &quot;a&quot;, encoding=&#39;utf-8&#39;) as f:<br>            for i, citation in enumerate(citations, start=1):<br>                f.write(f&quot;{i}. {citation}\n\n&quot;)<br><br>    except Exception as e:<br>        progress_data[&quot;status&quot;] = &quot;error&quot;<br>        progress_data[&quot;error&quot;] = str(e)</pre><p>The <em>“download_papers_background” </em>function starts the corresponding download function in a separate thread, depending on the selected database. The progress is saved in a global dictionary <em>“download_progess”</em>.</p><pre>def download_papers_background(download_id, database, query, year, num_papers):<br>    global current_process<br>    progress_data = download_progress.get(download_id, {})<br>    progress_data[&quot;status&quot;] = &quot;running&quot;<br>    progress_data[&quot;abort&quot;] = False<br>    download_progress[download_id] = progress_data<br><br>    db_folder = os.path.join(DOWNLOAD_ROOT, f&quot;{database}_{re.sub(r&#39;[^A-Za-z0-9]+&#39;, &#39;_&#39;, query)}_{year}&quot;)<br><br>    try:<br>        if database == &quot;pedocs&quot;:<br>            download_pedocs(query, year, num_papers, db_folder, progress_data)<br>        elif database == &quot;arxiv&quot;:<br>            download_arxiv(query, year, num_papers, db_folder, progress_data)<br>        elif database == &quot;eric&quot;:<br>            download_eric_selenium(query, year, num_papers, db_folder, progress_data)<br>        else:<br>            progress_data[&quot;status&quot;] = &quot;error&quot;<br>            progress_data[&quot;error&quot;] = &quot;Unknown database.&quot;<br>    except Exception as e:<br>        progress_data[&quot;status&quot;] = &quot;error&quot;<br>        progress_data[&quot;error&quot;] = str(e)<br>    finally:<br>        with process_lock:<br>            current_process = None</pre><h4>DATA ANALYSIS (word processing and LLM queries)</h4><p>In this part of FACTS, the publications downloaded in the first part are analyzed using a locally installed LLM. In addition to the databases already mentioned, users of the app can also upload and analyze scientific papers from other sources via a built-in PDF upload <em>“upload_pdfs”</em>. The module <em>“fitz”</em> (PyMuPDF) is used to read the text from all pages of the PDF. In order to obtain a clean continuous text, superfluous line breaks, page numbers and other disruptive elements are removed <em>“clean_text”</em>.</p><pre>def upload_pdfs():<br>    if &#39;files&#39; not in request.files:<br>        return jsonify({&quot;error&quot;: &quot;No files found.&quot;}), 400<br>    files = request.files.getlist(&#39;files&#39;)<br>    timestamp = datetime.now().strftime(&quot;%Y-%m-%d_%H-%M-%S&quot;)<br>    upload_folder = os.path.join(DOWNLOAD_ROOT, f&quot;upload_{timestamp}&quot;)<br>    os.makedirs(upload_folder, exist_ok=True)<br>    saved_files = []<br>    for file in files:<br>        if file.filename.endswith(&#39;.pdf&#39;):<br>            file_path = os.path.join(upload_folder, file.filename)<br>            file.save(file_path)<br>            saved_files.append(file.filename)<br>    return jsonify({&quot;message&quot;: &quot;Files uploaded&quot;, &quot;folder&quot;: os.path.basename(upload_folder), &quot;files&quot;: saved_files})<br><br>def clean_text(text):<br>    text = re.sub(r&#39;\nPage \d+\n|\n\d+\n&#39;, &#39;&#39;, text)<br>    text = re.sub(r&#39;\n+&#39;, &#39; &#39;, text)<br>    text = re.sub(r&#39;\s+&#39;, &#39; &#39;, text).strip()<br>    text = re.sub(r&#39;\d+\.&#39;, &#39;&#39;, text)<br>    return text</pre><p>The LLM query is executed by the <em>“query_llm_via_cli” </em>function, which starts a sub-process that sends the text to an LLM via the command line interface (CLI), in this case with the Ollama tool and the specially configured LLM llama3.1p, and returns its response [3]. The <em>“validate_llm_response”</em> function checks the LLM’s response to see whether it meets the expectations (e.g. minimum length, no empty responses) and reformats it if necessary.</p><pre>def query_llm_via_cli(input_text):<br>    try:<br>        process = subprocess.Popen(<br>            [&quot;ollama&quot;, &quot;run&quot;, &quot;llama3.1p&quot;],<br>            stdin=subprocess.PIPE,<br>            stdout=subprocess.PIPE,<br>            stderr=subprocess.PIPE,<br>            text=True,<br>            encoding=&#39;utf-8&#39;,<br>            errors=&#39;ignore&#39;,<br>            bufsize=1<br>        )<br>        stdout, stderr = process.communicate(input=f&quot;{input_text}\n&quot;, timeout=180)<br>        if process.returncode != 0:<br>            logging.error(f&quot;LLM errors: {stderr.strip()}&quot;)<br>            return &quot;&quot;<br>        response = re.sub(r&#39;\x1b\[.*?m&#39;, &#39;&#39;, stdout)<br>        logging.info(f&quot;LLM raw answer: {response.strip()}&quot;)<br>        return response.strip()<br>    except subprocess.TimeoutExpired:<br>        process.kill()<br>        return &quot;Timeout for the model request&quot;<br>    except Exception as e:<br>        return f&quot;An unexpected error has occurred: {str(e)}&quot;<br><br>def validate_llm_response(response, expected_count):<br>    response = re.sub(r&#39;(?i)^Here\s+are\s+the\s+answers\s+to\s+the\s+questions:\s*[\n\r]*&#39;, &#39;&#39;, response)<br>    response = re.sub(r&#39;(?im)^Question\s+\d+:\s*&#39;, &#39;&#39;, response)<br>    <br>    if not response.strip():<br>        return &quot;\n&quot;.join(f&quot;{i+1}. NO ANSWER!&quot; for i in range(expected_count))<br>    <br>    if expected_count == 1:<br>        answer = response.strip()<br>        if len(answer.split()) &lt; 3:<br>            return &quot;1. NO ANSWER!&quot;<br>        else:<br>            return &quot;1. &quot; + answer  <br>    else:<br>        lines = [re.sub(r&#39;^\d+\.\s*&#39;, &#39;&#39;, line.strip()) for line in response.splitlines() if line.strip()]<br>        invalid_keywords = [<br>            &quot;no answer&quot;, &quot;no answer&quot;, &quot;no&quot;, &quot;irrelevant&quot;, &quot;not specified&quot;,<br>            &quot;no relevance&quot;, &quot;no data&quot;, &quot;not explicitly mentioned&quot;, &quot;I can&#39;t&quot;, &quot;I can only&quot;<br>        ]<br>        validated_answers = []<br>        for i in range(expected_count):<br>            if i &lt; len(lines):<br>                answer = lines[i]<br>                if any(keyword in answer.lower() for keyword in invalid_keywords) or len(answer.split()) &lt;= 2:<br>                    validated_answers.append(&quot;NO ANSWER!&quot;)<br>                else:<br>                    validated_answers.append(answer)<br>            else:<br>                validated_answers.append(&quot;NO ANSWER!&quot;)<br>        return &quot;\n&quot;.join(f&quot;{i+1}. {ans}&quot; for i, ans in enumerate(validated_answers))</pre><p>To adjust the parameters of certain models, you can create a model file and thereby generate a special variant of an LLM. Two variants of the llama3.1 model from Meta were created for FACTS. The variant <em>“p”</em> is used in the second component and the variant <em>“p2”</em> in the third component. The parameters TEMPERATURE, SEED and TOP_P were adjusted in order to achieve reproducible results. The <em>“temperature”</em> controls the <em>“creativity”</em> of the model and the randomness of the selection from the probability distribution. The lower the value, the more likely it is that tokens with a high probability will be used. The parameter <em>“top_p”</em>, on the other hand, determines how large the proportion of the most probable tokens should be that are taken into account when answering. The starting value for the random generator is defined by the <em>“seed”</em> parameter. A fixed value leads to the same data delivering the same results. Why is this adjustment necessary? The following figure illustrates the problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EnTDlHYmBEvCn2ImPcQncg.png" /><figcaption>Problem of non-deterministic LLMs (<em>Image by author)</em></figcaption></figure><p>A section can provide an answer in one case and not in another. LLMs work probabilistically, i.e. they are not deterministic by default. However, in order to achieve reproducible (deterministic) results, certain parameters of the model must be adjusted to reduce the variability of the outputs. Even with deterministic parameters, a certain degree of variability remains. To reconfigure a model, a model file is used, which is structured as follows.</p><pre>FROM llama3.1<br><br>SYSTEM Please strictly follow the instructions of the transmitted context.<br><br>PARAMETER temperature 0<br>PARAMETER seed 42<br>PARAMETER top_p 1<br>PARAMETER stop &quot;&lt;|start_header_id|&gt;&quot;<br>PARAMETER stop &quot;&lt;|end_header_id|&gt;&quot;<br>PARAMETER stop &quot;&lt;|eot_id|&gt;&quot;<br>PARAMETER stop &quot;&lt;|reserved_special_token&gt;&quot;</pre><p>First, the model (in this case, llama3.1) that forms the basis is defined. Then the system message is defined and finally the parameters that are to be adjusted. The following command creates the configured version of the model.</p><pre>ollama create llama3.1p -f PATH\modelfile.txt</pre><p>As the analyzed texts are of different lengths, the text is divided into sections (chunks) of a maximum of approx. 4000 characters <em>“split_text_by_sentences”</em>. Each text section is then sent to the LLM together with a context query, the response is validated and finally saved in an output file <em>“analyze_long_text_in_chunks_and_save”</em>.</p><pre>def split_text_by_sentences(text, chunk_size=4000):<br>    sentences = re.split(r&#39;(?&lt;=[.!?]) +&#39;, text) <br>    chunks = []<br>    current_chunk = &quot;&quot;<br>    for sentence in sentences:<br>        if len(current_chunk) + len(sentence) &gt; chunk_size:<br>            chunks.append(current_chunk.strip())<br>            current_chunk = sentence<br>        else:<br>            current_chunk += &quot; &quot; + sentence<br>    if current_chunk:<br>        chunks.append(current_chunk.strip())<br>    return chunks<br><br>def analyze_long_text_in_chunks_and_save(cleaned_text, context_query, output_file, expected_count):<br>    system_message = (&quot;System: You are an intelligent assistant. Please only answer &quot;<br>                      &quot;concise and correct. Do not use introductions, meta-answers, &quot;<br>                      &quot;Numbering or repeating the question in your answer.&quot;)<br>    full_prompt_header = f&quot;{system_message}\n\n{context_query}&quot;<br>    <br>    chunks = split_text_by_sentences(cleaned_text, chunk_size=4000)<br>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:<br>        for i, chunk in enumerate(chunks):<br>            if not chunk.strip():<br>                result_text = &quot;\n\nResult for section {}:\n{}&quot;.format(<br>                    i+1, &quot;\n&quot;.join(f&quot;{j+1}. NO ANSWER!&quot; for j in range(expected_count))<br>                )<br>                f.write(result_text)<br>                continue<br><br>            logging.info(f&quot;Progress: Section {i+1}/{len(chunks)} ({(i+1)/len(chunks)*100:.2f}%)&quot;)<br>            prompt = f&quot;{full_prompt_header}\n\nText section {i+1}:\n{chunk}&quot;<br>            logging.info(f&quot;Send section {i+1}/{len(chunks)} for analysis...&quot;)<br>            analysis_result = query_llm_via_cli(prompt)<br>            validated_result = validate_llm_response(analysis_result, expected_count)<br>            result_text = f&quot;\n\nResult for section {i+1}:\n{validated_result}\n&quot;<br>            f.write(result_text)<br>    return output_file</pre><p>Depending on the selected directory, the <em>“run_analysis”</em> function iterates over all PDF files, extracts and cleans the text, performs the LLM query and saves the analysis results. The progress of the process is also updated here <em>“analysis_progress”</em>.</p><pre>def run_analysis(analysis_id, pdf_directory, context_query, expected_count):<br>    global current_process<br>    try:<br>        pdf_files = [f for f in os.listdir(pdf_directory) if f.endswith(&#39;.pdf&#39;)]<br>        total_files = len(pdf_files)<br>        <br>        pdf_dir_name = os.path.basename(pdf_directory)<br>        current_date = datetime.now().strftime(&quot;%Y-%m-%d&quot;)<br>        analysis_output_folder = os.path.join(DATA_ROOT, f&quot;{pdf_dir_name}_{current_date}&quot;)<br>        if not os.path.exists(analysis_output_folder):<br>            os.makedirs(analysis_output_folder)<br>        <br>        results_summary = []<br>        for idx, filename in enumerate(pdf_files):<br>            pdf_path = os.path.join(pdf_directory, filename)<br>            logging.info(f&quot;Process {filename} ...&quot;)<br>            <br>            extracted_text, page_count = extract_text_from_pdf(pdf_path)<br>            cleaned = clean_text(extracted_text)<br>            <br>            analysis_output_path = os.path.join(analysis_output_folder, f&quot;analysis_result_paper{idx+1}.txt&quot;)<br>            analyze_long_text_in_chunks_and_save(cleaned, context_query, analysis_output_path, expected_count)<br>            <br>            results_summary.append(f&quot;Analysis for {filename} completed (Result in {analysis_output_path}).&quot;)<br>            <br>            progress = int(((idx + 1) / total_files) * 100)<br>            analysis_progress[analysis_id][&quot;percent&quot;] = progress<br>            logging.info(f&quot;Analysis progress: {progress}%&quot;)<br>        <br>        analysis_progress[analysis_id][&quot;status&quot;] = &quot;completed&quot;<br>        analysis_progress[analysis_id][&quot;result&quot;] = &quot;\n&quot;.join(results_summary)<br>    finally:<br>        with process_lock:<br>            current_process = None</pre><h4>DATA EVALUATION (topic modelling and visualization)</h4><p>In this part of FACTS, the evaluation (sediment analysis) of the data and the answers found to the questions posed is carried out quantitatively and visually. Stop words are loaded by NLTK. It is also possible to create a user-defined list of stop words. The texts are broken down into individual words and the stop words are removed <em>“tokenize_texts”</em>.</p><pre>def tokenize_texts(text_data):<br>    tokenized_texts = [<br>        [word for word in text.lower().split() if word not in custom_stopwords]<br>        for text in text_data<br>    ]<br>    return tokenized_texts</pre><p>The functions <em>“perform_lda”</em> and <em>“find_optimal_num_topics” </em>perform LDA topic modeling with Gensim. The optimal number of topics is determined on the basis of coherence values. A value from 0.5 is acceptable. A value of over 0.6 can be interpreted as good or very good. The Silhouette score was still used in previous tests. However, as this is unsuitable for LDA because it is based on probability distributions, the Coherence Score was used.</p><pre>def perform_lda(text_data, num_topics, alpha_value=&#39;auto&#39;, beta_value=&#39;auto&#39;):<br>    tokenized_texts = tokenize_texts(text_data)<br>    dictionary = Dictionary(tokenized_texts)<br>    corpus = [dictionary.doc2bow(text) for text in tokenized_texts]<br>    lda_model = LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics, random_state=42,<br>                         iterations=100, passes=10, alpha=alpha_value, eta=beta_value)<br>    return lda_model, dictionary, corpus, tokenized_texts<br><br>def find_optimal_num_topics(texts, min_topics=2, max_topics=15, step=1, alpha_value=&#39;auto&#39;, beta_value=&#39;auto&#39;):<br>    tokenized_texts = tokenize_texts(texts)<br>    dictionary = Dictionary(tokenized_texts)<br>    corpus = [dictionary.doc2bow(text) for text in tokenized_texts]<br><br>    coherence_scores = []<br>    for num_topics in range(min_topics, max_topics + 1, step):<br>        lda_model = LdaModel(<br>            corpus=corpus, <br>            id2word=dictionary, <br>            num_topics=num_topics, <br>            random_state=42, <br>            iterations=100,<br>            passes=10,<br>            alpha=alpha_value,  <br>            eta=beta_value  <br>        )<br>        coherence_model = CoherenceModel(model=lda_model, texts=tokenized_texts, dictionary=dictionary, coherence=&#39;c_v&#39;)<br>        coherence_score = coherence_model.get_coherence()<br>        coherence_scores.append(coherence_score)<br>        print(f&quot;Number of topics: {num_topics} -- Coherence: {coherence_score:.4f}&quot;)<br><br>    optimal_topics = range(min_topics, max_topics + 1, step)[np.argmax(coherence_scores)]<br>    print(f&quot;Optimal number of topics: {optimal_topics}&quot;)<br>    return optimal_topics</pre><p>The LDA results are converted by the <em>“visualize_lda”</em> function into an interactive HTML visualization using <em>“pyLDAvis”</em>. Word clouds are also created for the individual topics using <em>“plot_wordclouds”</em> to visually highlight the most common terms in the topics. In addition, an interactive knowledge graph is created using <em>“NetworkX” </em>and <em>“Plotly”</em>. In these, the topics are displayed as nodes that are linked to the most important terms. The nodes are colored according to relevance and connection strength. This allows you to quickly recognize which terms play a role in several topics, which is important for the general interpretation of the data.</p><pre>def visualize_lda(lda_model, dictionary, corpus, question_id, p_w=None, output_directory=None):<br>    if output_directory is None:<br>        output_directory = os.getcwd()<br>    try:<br>        lda_vis = pyLDAvis.gensim_models.prepare(lda_model, corpus, dictionary)<br>        output_html_path = os.path.join(output_directory, f&#39;lda_visualization_question_{question_id}.html&#39;)<br>        pyLDAvis.save_html(lda_vis, output_html_path)<br>        print(f&quot;Visualization for question {question_id} saved as HTML: {output_html_path}&quot;)<br>    except Exception as e:<br>        print(f&quot;Visualization error for question {question_id}: {e}&quot;)<br><br>def plot_wordclouds(lda_model, dictionary, lambda_val=1.0, num_words=30, p_w=None, output_directory=None):<br>    if output_directory is None:<br>        output_directory = os.getcwd()<br>    if p_w is None:<br>        total_count = sum(dictionary.cfs.values())<br>        p_w = {dictionary[token_id]: count / total_count for token_id, count in dictionary.cfs.items()}<br>    num_topics = lda_model.num_topics<br>    for topic_idx in range(num_topics):<br>        top_terms = get_top_terms_for_topic(lda_model, topic_idx, dictionary, p_w, lambda_val=lambda_val, topn=num_words)<br>        topic_terms = dict(lda_model.get_topic_terms(topic_idx, topn=num_words))<br>        word_freq = {}<br>        for word in top_terms:<br>            word_id = dictionary.token2id.get(word)<br>            if word_id is not None:<br>                word_freq[word] = topic_terms.get(word_id, 1e-12)<br>        wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;).generate_from_frequencies(word_freq)<br>        plt.figure(figsize=(10, 5))<br>        plt.imshow(wordcloud, interpolation=&#39;bilinear&#39;)<br>        plt.title(f&quot;Wordcloud für Topic {topic_idx + 1} (λ={lambda_val})&quot;)<br>        plt.axis(&#39;off&#39;)<br>        output_path = os.path.join(output_directory, f&quot;wordcloud_topic_{topic_idx + 1}_lambda_{lambda_val}.png&quot;)<br>        plt.savefig(output_path)<br>        plt.close()<br>        print(f&quot;Wordcloud for Topic {topic_idx + 1} (λ={lambda_val}) stored: {output_path}&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KcrxUb3iG3HVjHmo3WcgKg.gif" /><figcaption>(<em>GIF by author)</em></figcaption></figure><p>The data is also interpreted using an LLM (llama3.1p2). The default model can also be used here. To reduce meta comments, however, it is recommended to reduce the temperature. The functions <em>“get_combined_prompt” </em>and <em>“query_ollama”</em> formulate a combined prompt, which contains the most important terms, according to frequency (λ = 1) and exclusivity (λ = 0), and questions and forwards this to the LLM, whereby a content interpretation of the topics is generated.</p><pre>def get_combined_prompt(top_terms_freq, top_terms_excl, question_text):<br>    prompt = f&quot;&quot;&quot;<br>For the following question: &#39;{question_text}&#39; after LDA topic modeling, two sets of top terms were identified:<br><br>- Based on frequency (λ = 1): {&#39;, &#39;.join(top_terms_freq)}<br>- Based on exclusivity (λ = 0): {&#39;, &#39;.join(top_terms_excl)}<br><br>Please interpret these two sets of top terms according to the following scheme:<br><br>1) Introduction: Brief introduction to the topic.<br>2) Most common terms (λ = 1): Explain why these terms appear particularly frequently and their relevance to answering the question.<br>3) Most exclusive terms (λ = 0): Explain what these particularly exclusive terms mean and how they complement or specify the topic.<br>4) Conclusion: Summarize how the two sets of terms together help answer the question.<br><br>Please provide your answer in clear, flowing text without Markdown, stars, bullet points, or other special formatting.<br>&quot;&quot;&quot;<br>    return prompt<br><br>def query_ollama(input_text, model=&quot;llama3.1p&quot;):<br>    try:<br>        process = subprocess.Popen(<br>            [&quot;ollama&quot;, &quot;run&quot;, model],<br>            stdin=subprocess.PIPE,<br>            stdout=subprocess.PIPE,<br>            stderr=subprocess.PIPE,<br>            text=True,<br>            encoding=&quot;utf-8&quot;,<br>            errors=&quot;replace&quot;<br>        )<br>        stdout, stderr = process.communicate(input=input_text, timeout=180)<br>        if process.returncode != 0:<br>            print(f&quot;Error during model request: {stderr.strip()}&quot;)<br>            return &quot;Interpretation not possible&quot;<br>        return stdout.strip()<br>    except Exception as e:<br>        print(f&quot;An error has occurred: {e}&quot;)<br>        return &quot;Error retrieving interpretation&quot;</pre><p>All the steps described are linked by the <em>“lda_analysis_with_interpretation”</em> function and the results are saved as an HTML or .TXT file in the <em>“sediment_analysis” </em>analysis folder.</p><pre>def lda_analysis_with_interpretation(file_path, question_id, num_topics, model=&quot;llama3.1p&quot;, output_directory=None):<br>    try:<br>        data = pd.read_csv(file_path, encoding=&#39;utf-8&#39;)<br>        text_data = data[f&#39;Questions {question_id} Answers&#39;].dropna().tolist()<br>        if not text_data:<br>            print(f&quot;No data for LDA analysis of question {question_id}.&quot;)<br>            return<br><br>        optimal_topics = num_topics<br>        lda_model, dictionary, corpus, tokenized_texts = perform_lda(text_data, optimal_topics)<br>        total_count = sum(dictionary.cfs.values())<br>        p_w = {dictionary[token_id]: count / total_count for token_id, count in dictionary.cfs.items()}<br><br>        topic_totals = np.zeros(lda_model.num_topics)<br>        for doc in corpus:<br>            doc_topics = lda_model.get_document_topics(doc, minimum_probability=0)<br>            for topic_id, prob in doc_topics:<br>                topic_totals[topic_id] += prob<br>        sorted_topic_indices = np.argsort(topic_totals)[::-1]<br><br>        interpretations = []<br>        for cluster_idx, topic_idx in enumerate(sorted_topic_indices):<br>            top_terms_freq = get_top_terms_for_topic(lda_model, topic_idx, dictionary, p_w, lambda_val=1.0, topn=30)<br>            top_terms_excl = get_top_terms_for_topic(lda_model, topic_idx, dictionary, p_w, lambda_val=0.0, topn=30)<br><br>            combined_prompt = get_combined_prompt(top_terms_freq, top_terms_excl, f&quot;Question {question_id}&quot;)<br>            combined_output = query_ollama(combined_prompt, model)<br>            interpretation_sections = combined_output.split(&quot;\n&quot;, 1)<br>            interpretations.append({<br>                &quot;Topic&quot;: cluster_idx + 1,<br>                &quot;Top Terms (Frequency, λ=1)&quot;: &#39;, &#39;.join(top_terms_freq),<br>                &quot;Top Terms (Exclusivity, λ=0)&quot;: &#39;, &#39;.join(top_terms_excl),<br>                &quot;Interpretation&quot;: &quot;\n&quot;.join(interpretation_sections).strip()<br>            })<br><br>        if output_directory is None:<br>            output_directory = os.path.join(ANALYSIS_ROOT, &quot;evaluation_&quot; + datetime.now().strftime(&quot;%Y-%m-%d_%H-%M-%S&quot;))<br>            if not os.path.exists(output_directory):<br>                os.makedirs(output_directory)<br><br>        visualize_lda(lda_model, dictionary, corpus, question_id, p_w=p_w, output_directory=output_directory)<br>        plot_wordclouds(lda_model, dictionary, lambda_val=1.0, num_words=30, p_w=p_w, output_directory=output_directory)<br>        plot_wordclouds(lda_model, dictionary, lambda_val=0.0, num_words=30, p_w=p_w, output_directory=output_directory)<br><br>        create_knowledge_graph(<br>            lda_model=lda_model,<br>            dictionary=dictionary,<br>            corpus=corpus,<br>            sorted_topic_indices=sorted_topic_indices,<br>            num_topics=optimal_topics,<br>            lambda_val=1.0,<br>            p_w=p_w,<br>            output_html=os.path.join(output_directory, f&quot;knowledge_graph_question_{question_id}_lambda_1.html&quot;)<br>        )<br>        create_knowledge_graph(<br>            lda_model=lda_model,<br>            dictionary=dictionary,<br>            corpus=corpus,<br>            sorted_topic_indices=sorted_topic_indices,<br>            num_topics=optimal_topics,<br>            lambda_val=0.0,<br>            p_w=p_w,<br>            output_html=os.path.join(output_directory, f&quot;knowledge_graph_question_{question_id}_lambda_0.html&quot;)<br>        )<br><br>        output_txt_path = os.path.join(output_directory, f&quot;interpretations_question_{question_id}.txt&quot;)<br>        save_interpretations_to_txt(interpretations, output_txt_path)<br><br>        print(&quot;LDA analysis completed. Results in folder:&quot;, output_directory)<br>    except Exception as e:<br>        print(f&quot;Error in lda_analysis_with_interpretation for question {question_id}: {e}&quot;)</pre><h4>FACTS V2 PYTHON SCRIPT [GITHUB]</h4><p>The application can be installed via the following <a href="https://github.com/stefanpietrusky/FACTSV2">GitHub repository</a>. The source code is available in German and English. The model files for the two LLM variants are also available. Other LLMs can also be used, but their output may not be processed correctly by the application, in which case the parsing/regex must be adapted accordingly.</p><h4>CONCLUSSION</h4><p>The tests of the FACTS app carried out so far have already shown good to very good results. From the first version to the freely available version, numerous optimizations have already been implemented. Through the combination of asynchronous background processes, modern NLP methods and interactive visualizations, FACTS is an effective tool for filtering and analyzing large amounts of text from scientific sources. The modular and well-structured architecture makes it possible to respond flexibly to different data sources and analysis requirements. FACTS automates the pure collection of data through to content analysis and interpretation of the results in an application. A further development would be the implementation of APIs in order to be able to use GPU server instances, which would reduce the processing speed and process time if no powerful end devices are available. As part of further tests, the app is to be further developed with regard to additional databases and analysis methods.</p><p>[1] Pietrusky, S. (2024). Automatic answering of scientific questions using the FACTS-V1 framework: New methods in research to increase efficiency through the use of generative AI.<strong> </strong>ARXIV CS.DL <a href="https://arxiv.org/abs/2412.07794">Link</a></p><p>[2] <a href="https://github.com/mozilla/geckodriver/releases">https://github.com/mozilla/geckodriver/releases</a></p><p>[3] <a href="https://ollama.com/library/llama3.1/blobs/667b0c1932bc">https://ollama.com/library/llama3.1/blobs/667b0c1932bc</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TJp_V5aLi1SuN11MGdhXHw.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a16cdac811b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science-collective/facts-v2-filtering-and-analysis-of-content-in-textual-sources-1a16cdac811b">How I automatically find numerous answers for any given question.</a> was originally published in <a href="https://medium.com/data-science-collective">Data Science Collective</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IEC V1.5: Individual Educational Chatbot.]]></title>
            <link>https://levelup.gitconnected.com/iec-v1-5-individual-educational-chatbot-f859456ec760?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/f859456ec760</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[iec]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 02:17:25 GMT</pubDate>
            <atom:updated>2025-05-22T20:10:14.865Z</atom:updated>
            <content:encoded><![CDATA[<h3>Individual learning support made easy</h3><h4><strong>IEC V1.5: Individual Educational Chatbot. </strong>A tool for individual exchange with PDF files and websites</h4><p>This article is about setting up the IEC V1.5 application that can be executed locally. The interface of the application is not created using a framework such as Gradio or Streamlit, but via HTML, CSS and JS. The advantage of this is that the design of the application can be customized more quickly. In IEC, users can implement PDF files and/or URLs as a source. The text extracted from the sources can be viewed or hidden. The questions are only answered on the basis of the sources used, which means that the chatbot is not restricted by its limited knowledge database, starts hallucinating in the event of ignorance or does not provide an answer at all for current topics. The level of the answer can also be set individually. The basic structure is shown in the following illustration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZZhRapHwOizxabaSyt8HLw.png" /><figcaption>Structure and mode of operation IEC V1.5 (Image by author)</figcaption></figure><p>This function was rated very positively in an initial IEC trial [1]. Communication between the application and LLM works via CLI and Ollama. The model currently used is Llama3.1 (8b) from Meta. However, other models can also be used via the model management system or, depending on the available computing power, models with more parameters. As the installation of Ollama and an LLM or SLM has already been described in various articles [2], it will not be discussed again here. The functionality of the code is briefly described below. The application can be installed via the GitHub repository linked below and currently looks as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UHttqCZmYejLbwqohveDGA.gif" /><figcaption>How the IEC V1.5 app works (GIF by author)</figcaption></figure><h4>REQUIREMENTS</h4><p>Flask and other libraries (e.g. <em>“requests”</em> to retrieve content from the web, <em>“fitz”</em> to extract text from PDF files, etc.) are imported to start the web application. For the tokenization of the texts for the LLM, <em>“tiktoken”</em> a tokenizer from OpenAI (GPT-2) is used.</p><pre>from flask import Flask, request, jsonify, Response<br>import os<br>import subprocess<br>import re<br>import requests<br>from readability import Document<br>import fitz<br>from bs4 import BeautifulSoup<br>import tiktoken<br><br>app = Flask(__name__)<br>os.environ[&quot;KMP_DUPLICATE_LIB_OK&quot;] = &quot;TRUE&quot;</pre><h4>TOKENIZATION AND HELP FUNCTIONS</h4><p>This is followed by tokenization and text splitting. The maximum input length of the Llama3.1 model from Meta is 131072 tokens (llama.context_length: 131072 [3]). Depending on which model is used, this value must be adjusted. The extracted text is converted into tokens <em>“tokenize-text(text)”</em> and then back to text <em>“detokenize_text(tokens)”</em>. If the text exceeds the token limit, it is split into several blocks to be processed by the LLM.</p><pre>TOKEN_LIMIT = 131072<br>enc = tiktoken.get_encoding(&quot;gpt2&quot;)<br><br>def tokenize_text(text):<br>    return enc.encode(text)<br><br>def detokenize_text(tokens):<br>    return enc.decode(tokens)<br><br>def split_text_into_blocks(text, max_tokens):<br>    tokens = tokenize_text(text)<br>    for i in range(0, len(tokens), max_tokens):<br>        yield detokenize_text(tokens[i : i + max_tokens])</pre><h4>FUNCTIONS FOR EXTRACTING CONTENT</h4><p>The next step is the extraction of content. The content of the website is retrieved from the HTML content of the website using “<em>requests.get(url)”</em>. HTML tags are removed by the “<em>BeautifulSoup</em>” library so that only the plain text is transmitted. The uploaded PDF file is read in as a stream. The file is opened by <em>“fitz.open(stream=pdf_data, filetype=”pdf”)”</em> directly from memory and extracted page by page <em>“.get_text()”</em>. Several spaces are removed by <em>“re.sub()”</em>. The uploaded media (URL/PDF files) and the content extracted from them are combined into a single string <em>“all_content.strip()”</em>.</p><pre>def get_readable_content(url):<br>    try:<br>        response = requests.get(url, timeout=30)<br>        response.raise_for_status()<br>        doc = Document(response.text)<br>        readable_html = doc.summary()<br>        soup = BeautifulSoup(readable_html, &#39;html.parser&#39;)<br>        readable_text = soup.get_text()<br>        return re.sub(r&#39;\s+&#39;, &#39; &#39;, readable_text).strip()<br>    except requests.exceptions.RequestException as e:<br>        return f&quot;Error fetching content: {str(e)}&quot;<br><br>def extract_text_from_pdf(file):<br>    text = &quot;&quot;<br>    pdf_data = file.read()<br>    try:<br>        with fitz.open(stream=pdf_data, filetype=&quot;pdf&quot;) as pdf:<br>            for page in pdf:<br>                text += page.get_text()<br>    except Exception as e:<br>        return f&quot;Error reading PDF: {str(e)}&quot;<br>    cleaned_text = re.sub(r&#39;\s*\n\s*&#39;, &#39; &#39;, text)<br>    cleaned_text = re.sub(r&#39;\s{2,}&#39;, &#39; &#39;, cleaned_text)<br>    return cleaned_text.strip()<br><br>def extract_content(url_input, pdf_files):<br>    all_content = &quot;&quot;<br>    if url_input:<br>        urls = [url.strip() for url in url_input.split(&quot;,&quot;)]<br>        for url in urls:<br>            if url.startswith(&quot;http&quot;):<br>                all_content += get_readable_content(url) + &quot;\n&quot;<br>    if pdf_files:<br>        for pdf_file in pdf_files:<br>            pdf_text = extract_text_from_pdf(pdf_file)<br>            all_content += pdf_text + &quot;\n&quot;<br>    return all_content.strip() or &quot;No content extracted from the provided inputs.&quot;</pre><h4>FUNCTIONS FOR ANSWERING QUESTIONS</h4><p>The user’s question is sent to the locally installed LLM via <em>“subprocess.run()”</em>. The model currently has 60 seconds <em>“timeout=60”</em> for the answer. If the LLM takes too long, an error is output.</p><pre>def query_llama_via_cli(input_text):<br>    try:<br>        result = subprocess.run(<br>            [&quot;ollama&quot;, &quot;run&quot;, &quot;llama3.1&quot;],<br>            input=f&quot;{input_text}\n&quot;,<br>            stdout=subprocess.PIPE,<br>            stderr=subprocess.PIPE,<br>            text=True,<br>            encoding=&#39;utf-8&#39;,<br>            timeout=60<br>        )<br>        if result.returncode != 0:<br>            return f&quot;Error in the model request: {result.stderr.strip()}&quot;<br>        response = re.sub(r&#39;\x1b\[.*?m&#39;, &#39;&#39;, result.stdout)<br>        return response.strip()<br>    except subprocess.TimeoutExpired:<br>        return &quot;The model request timed out. Please try again.&quot;<br>    except Exception as e:<br>        return f&quot;An unexpected error has occurred: {str(e)}&quot;</pre><p>As already mentioned, the individual definition of the competence level of the answer was very well received during the first test [1]. Depending on the level selected by the user, the model’s answer is simplified or more detailed. Such a function is not included in many commercial applications. If you want a <em>“simpler”</em> answer, the question must be formulated accordingly. With IEC, the answer to the same question can be quickly and easily adjusted in terms of level. The extracted content is transmitted in blocks, and the answer is generated and assembled block by block.</p><pre>def generate_responses_from_blocks(competence_level, extracted_content, user_question):<br>    if not competence_level or not user_question.strip():<br>        return &quot;Please select a competence level and enter a question.&quot;<br>    if not extracted_content.strip():<br>        return &quot;No content extracted on which the answer could be based.&quot;<br>    <br>    if competence_level == &quot;Beginner&quot;:<br>        style_instruction = (<br>            &quot;Respond in very simple words, avoid technical terms, and keep the answer short and concise. &quot;<br>            &quot;Explain the topic in a way that even laypersons can understand.&quot;<br>        )<br>    elif competence_level == &quot;Intermediate&quot;:<br>        style_instruction = (<br>            &quot;Provide a clear and detailed response, occasionally using technical terms and offering a balanced explanation. &quot;<br>            &quot;Explain the topic so that the reader gains a good understanding without it becoming too technical.&quot;<br>        )<br>    elif competence_level == &quot;Advanced&quot;:<br>        style_instruction = (<br>            &quot;Provide a highly detailed and technical response, use specialized terminology, and offer a comprehensive analysis. &quot;<br>            &quot;Explain the topic at a high, academic level.&quot;<br>        )<br>    else:<br>        style_instruction = &quot;Respond based on the provided information.&quot;<br>    <br>    base_prompt = (<br>        f&quot;System Instruction: {style_instruction}\n\n&quot;<br>        &quot;Note: The following information serves only as a reference. Please phrase the answer in your own words and consider the desired style.\n\n&quot;<br>        &quot;Provided Information (Excerpt):\n&quot;<br>    )<br>    question_prompt = f&quot;\n\nQuestion: {user_question}\n\nAnswer:&quot;<br>    <br>    prompt_prefix_tokens = len(tokenize_text(base_prompt)) + len(tokenize_text(question_prompt))<br>    max_tokens_for_block = TOKEN_LIMIT - prompt_prefix_tokens<br><br>    responses = []<br>    for block in split_text_into_blocks(extracted_content, max_tokens_for_block):<br>        input_text = base_prompt + block + question_prompt<br>        response = query_llama_via_cli(input_text)<br>        responses.append(response)<br>    return &quot;\n&quot;.join(responses)</pre><p>Here is an example. To the question: <em>What this article is about?</em> (<a href="https://en.wikipedia.org/wiki/Named-entity_recognition">article</a>), the IEC provides the following answer depending on the level:</p><h4>Beginner answer:</h4><p>This article appears to be a collection of references and information related to named entity recognition (NER), which is a subtask of natural language processing (NLP) that involves identifying and categorizing entities mentioned in text into predefined categories such as names, locations, organizations, etc. The article cites various papers, articles, and workshops related to NER, including those focused on Twitter-specific challenges and noisy user-generated text. It also touches on topics like domain adaptation, crowdsourcing for gold standard development, and graph-based semi-supervised learning models. Overall, the purpose of this article seems to be to provide a comprehensive overview of the state-of-the-art in named entity recognition research, with a focus on Twitter-specific challenges and noisy user-generated text.</p><h4>Intermediate answer:</h4><p>This article is about Named Entity Recognition (NER), a subtask of natural language processing that involves identifying and categorizing named entities in text into predefined categories such as names of people, organizations, locations, etc. The article provides an overview of the NER task, its applications, challenges, and various techniques used to address them, including machine learning-based approaches, rule-based systems, and hybrid models. It also discusses the importance of high-quality gold standard development in clinical NLP and the use of crowdsourcing for this purpose. Additionally, the article touches on the topic of linking named entities to external knowledge resources such as Wikipedia. Overall, it provides a comprehensive review of the NER task and its related research areas.</p><h4>Advanced answer:</h4><p>This article appears to be a comprehensive review of the topic of named entity recognition (NER), which is a subtask in natural language processing that involves identifying and categorizing entities such as names, locations, organizations, dates, times, quantities, and monetary values mentioned in unstructured text.</p><p>The article covers various aspects of NER, including:</p><ul><li>The importance and applications of NER</li><li>Overview of different machine learning approaches to NER, including conditional random fields (CRFs), support vector machines (SVMs), and deep neural networks</li><li>Discussion of domain adaptation techniques for improving NER performance on noisy or unseen data</li><li>Review of various datasets and evaluation metrics used in NER tasks</li><li>Presentation of different methods for handling named entity linking and disambiguation</li></ul><p>Overall, the article provides a thorough overview of the field of NER and its current state-of-the-art approaches.</p><h4>DESIGN, STRUCTURE AND INTERACTIVITY</h4><p>The design (CSS_CONTENT), structure (HTML_CONTENT) and interactivity (JS_CONTENT) of the IEC are defined via the corresponding areas and can be customized. CSS, HTML and JS have been combined in one script to avoid having to switch back and forth between different files. Depending on personal preference, the parts can also be separated. Here are a few examples of how the application can be modified by changing the CSS, HTML or JS code. The spinner that is displayed when processing input can be customized using the CSS rule <em>“.spinner”</em>.</p><pre>.spinner {<br>  border: 8px solid #f3f3f3;<br>  border-top: 8px solid #3498db;<br>  border-radius: 50%;<br>  width: 50px;<br>  height: 50px;<br>  animation: spin 1s linear infinite;<br>  margin: 20px auto;<br>}<br><br>@keyframes spin {<br>  0% { transform: rotate(0deg); }<br>  100% { transform: rotate(360deg); }<br>}</pre><p>The model’s answer to the user’s question is displayed letter by letter using a typewriter effect. The <em>“speed”</em> can be adjusted using the corresponding function.</p><pre>  function typeWriterHTML(html, element, speed = 5) {<br>    let i = 0;<br>    let isTag = false; <br>    let output = &quot;&quot;;<br><br>    function type() {<br>      if (i &lt; html.length) {<br>        let char = html[i];<br>        output += char;<br>        <br>        if (char === &quot;&lt;&quot;) {<br>          isTag = true;<br>        }<br>        if (char === &quot;&gt;&quot;) {<br>          isTag = false;<br>        }<br>        <br>        element.innerHTML = output;<br>        <br>        i++;<br>        setTimeout(type, isTag ? 0 : speed);<br>      }<br>    }<br>    type();<br>  }</pre><p>The names of the buttons for the different competence levels can be customized via HTML.</p><pre>      &lt;div class=&quot;button-group&quot; id=&quot;competence-group&quot;&gt;<br>        &lt;button class=&quot;competence-button&quot; data-level=&quot;Beginner&quot;&gt;Beginner&lt;/button&gt;<br>        &lt;button class=&quot;competence-button selected&quot; data-level=&quot;Intermediate&quot;&gt;Intermediate&lt;/button&gt;<br>        &lt;button class=&quot;competence-button&quot; data-level=&quot;Advanced&quot;&gt;Advanced&lt;/button&gt;<br>      &lt;/div&gt;</pre><h4>FLASK API-Endpoints</h4><p>Finally, the Flask API endpoints are defined, which form the core functionalities of the app. Specifically, the user interface, processing of content and generation of answers to user questions.</p><pre>@app.route(&#39;/&#39;)<br>def index():<br>    return Response(HTML_CONTENT, mimetype=&#39;text/html&#39;)<br><br>@app.route(&#39;/styles.css&#39;)<br>def styles():<br>    return Response(CSS_CONTENT, mimetype=&#39;text/css&#39;)<br><br>@app.route(&#39;/script.js&#39;)<br>def script():<br>    return Response(JS_CONTENT, mimetype=&#39;application/javascript&#39;)<br><br>@app.route(&#39;/extract_content&#39;, methods=[&#39;POST&#39;])<br>def extract_content_endpoint():<br>    if request.content_type.startswith(&#39;multipart/form-data&#39;):<br>        url_input = request.form.get(&quot;urls&quot;, &quot;&quot;)<br>        pdf_files = request.files.getlist(&quot;pdfs&quot;)<br>    else:<br>        data = request.get_json()<br>        url_input = data.get(&quot;urls&quot;, &quot;&quot;)<br>        pdf_files = []<br>    extracted = extract_content(url_input, pdf_files)<br>    return jsonify({&quot;content&quot;: extracted or &quot;Kein Inhalt extrahiert.&quot;})<br><br>@app.route(&#39;/ask_question&#39;, methods=[&#39;POST&#39;])<br>def ask_question():<br>    data = request.get_json()<br>    competence_level = data.get(&quot;competence_level&quot;)<br>    extracted_content = data.get(&quot;content&quot;, &quot;&quot;)<br>    user_question = data.get(&quot;question&quot;, &quot;&quot;)<br>    if not competence_level or not user_question.strip():<br>        return jsonify({&quot;response&quot;: &quot;Please select a competence level and enter a question.&quot;})<br>    if not extracted_content.strip():<br>        return jsonify({&quot;response&quot;: &quot;No extracted content available.&quot;})<br>    response = generate_response_from_extracted_content(competence_level, extracted_content, &quot;&quot;, None, user_question)<br>    return jsonify({&quot;response&quot;: response})<br><br>@app.route(&#39;/clear_extracted&#39;, methods=[&#39;POST&#39;])<br>def clear_extracted():<br>    return jsonify({&quot;content&quot;: &quot;&quot;})<br><br>if __name__ == &#39;__main__&#39;:<br>    app.run(debug=True, host=&quot;0.0.0.0&quot;, port=5000) </pre><h4>APPLICATION POSSIBILITIES</h4><p>The IEC can be used in the private sphere to ask questions about the content of PDF files or websites, but also in educational institutions to provide individual support to learners. Specifically, by installing the IEC directly on the end devices, sharing it with others via the local network or making it available via the Internet using a tunneling tool (ngrok).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ddC94QqQ-Is_icnGGDj3-A.png" /><figcaption>Making IEC available via the Internet (Image by author)</figcaption></figure><p>The only thing that needs to be ensured here is that the PC from which the application is made available to the learners acts as a server. If the computing capacity of the PC is too low, the requests cannot be processed effectively. The application currently contains three different levels. These can be expanded together with the learners if necessary.</p><h4>IEC V1.5 PYTHON SCRIPT [GITHUB]</h4><p>The application can be installed via the following <a href="https://github.com/stefanpietrusky/IECV1.5">GitHub repository</a>.</p><h4>CONCLUSION</h4><p>This article showed how to create an interactive chatbot using HTML, CSS and JS without using well-known frameworks for web interfaces such as Dash, Streamlit, Gradio, Voila etc. Even if the LLMs used via Ollama are closed, the models can use their NLP function and corresponding content (PDF files and/or URLs) to provide information and thus enable an exchange on current topics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*j6DnTo7-FNuMgfQOD6HrGA.gif" /><figcaption>You can clap up to 50 times!</figcaption></figure><p>[1] Pietrusky, S. (2024). Promoting AI Literacy in Higher Education: Evaluating the IEC-V1 Chatbot for Personalized Learning and Educational Equity. ARXIV CS.CY <a href="https://doi.org/10.48550/arXiv.2412.16165">Link</a></p><p>[2] Pietrusky, S. (2024). How to talk to a PDF file without using proprietary models: CLI + Streamlit + Ollama. Towards Data Science. <a href="https://towardsdatascience.com/how-to-talk-to-a-pdf-file-without-using-proprietary-models-cli-streamlit-ollama-6c22437ed932">Link</a></p><p>[3] <a href="https://ollama.com/library/llama3.1/blobs/667b0c1932bc">https://ollama.com/library/llama3.1/blobs/667b0c1932bc</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*et9WBarVV_XYqUOR5CLwTg.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f859456ec760" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/iec-v1-5-individual-educational-chatbot-f859456ec760">IEC V1.5: Individual Educational Chatbot.</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ASCVIT V1: Automatic Statistical Calculation, Visualization and Interpretation Tool]]></title>
            <link>https://medium.com/data-science/ascvit-v1-automatic-statistical-calculation-visualization-and-interpretation-tool-aa910001a3a7?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa910001a3a7</guid>
            <category><![CDATA[streamlit]]></category>
            <category><![CDATA[hands-on-tutorials]]></category>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Mon, 16 Sep 2024 16:21:34 GMT</pubDate>
            <atom:updated>2025-05-13T07:23:58.192Z</atom:updated>
            <content:encoded><![CDATA[<h3>ASCVIT V1: Automatic Statistical Calculation, Visualization, and Interpretation Tool</h3><h4>Automated data analysis made easy: The first version of ASCVIT, the tool for statistical calculation, visualization, and interpretation</h4><p>During my studies, I attended a data science seminar and came into contact with the statistical programming language R for the first time. At the time, I was fascinated by the resulting potential uses. In the meantime, the statistical evaluation of data has become easier thanks to developments in the field of machine learning. Of course, a certain level of technical understanding is required, and you need to know what certain methods actually do. It is also necessary to know what data or input is required for certain methods to work at all or deliver meaningful results. In this article, I would like to discuss the development of a first version (V1) of a local app that can be used to automatically apply various statistical methods to any datasets. This is an open source project for educational and research purposes.</p><p>The data can be uploaded in either .csv or .xlsx format. The first version of the app provides a general data overview (data preview, data description, number of data points and categorization of variables), analyses in the field of descriptive statistics (histogram, boxplot, pairplot and correlation matrix), various hypothesis tests (t-test, ANOVA and chi-square test), regression analyses (linear, logistic and multivariate), time series analysis and supports various clustering methods (k-means, hierarchical and DBSCAN). The app is created using the Python framework Streamlit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c0i-eO0rYZ0zi9GvuI6d8A.png" /><figcaption>ASCVIT V1 Overview of analysis methods (Image by author)</figcaption></figure><p>Due to the modular structure of the code, further statistical procedures can be easily implemented. The code is commented, which makes it easier to find your way around. When the app is executed, the interface looks like this after a dataset has been uploaded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l5sGIdJ8_dZ0TJiIU9qhLA.png" /><figcaption>ASCVIT V1 Streamlit App (Image by author)</figcaption></figure><p>In addition to the automatic analysis in the various areas mentioned, a function has also been integrated that automatically analyses the statistically recorded values. The <em>“query_llm_via_cli” </em>function enables an exchange via CLI (command line interface) with an LLM using Ollama.</p><p>I have already explained this principle in an article published on <a href="https://medium.com/towards-data-science/how-to-talk-to-a-pdf-file-without-using-proprietary-models-cli-streamlit-ollama-6c22437ed932"><strong>Towards Data Science</strong></a> [1]. In the first version of the app, this functionality is only limited to the descriptive statistical analyses, but can also be transferred to the others. In concrete terms, this means that in addition to the automatic statistical calculation, the app also automatically interprets the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O6x20FDFAyjHeQ6jxxuykg.png" /><figcaption>ASCVIT V1 CLI + OLLAMA + LMS (Image by author)</figcaption></figure><h4>DATASET FOR TESTING THE APP</h4><p>If you do not have your own data available, there are various sites on the internet that provide datasets free of charge. The dataset used for the development and testing of the app comes from <a href="https://mavenanalytics.io/data-playground?accessType=open&amp;order=date_added%2Cdesc&amp;page=1&amp;pageSize=5"><strong>Maven Analytics</strong></a><strong> </strong>(License: ODC-BY) [2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WYLHliBsikwgDfb1DQC9-g.png" /><figcaption>MAVEN Analytics Data Playground (Screenshot by author)</figcaption></figure><p>There are numerous free datasets on the site. The data I have examined deals with the sales figures of video games in the period from 1976 to 2024. Specifically, it is the sales figures from North America, Japan, the EU, Africa and the rest of the world. A total of 64016 titles and their rating, genre, console, etc. are recorded.</p><p>Unfortunately, not all information is available for all titles. There are many NaN (Not a Number) values that cause problems when analysed in Python or distort specific statistical analyses. I will briefly discuss the cleansing of data records below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V6IrSFYIe5yJCGAug55x1Q.png" /><figcaption>Video Games Sales data from MAVEN Analytics (Screenshot by author)</figcaption></figure><h4>CLEAN UP THE DATASET</h4><p>You can either clean the dataset before loading it into the app using a separate script, or you can perform the cleanup directly in the app. For the application in this article, I have implemented data cleansing directly in the app. If you want to clean up data records beforehand, you can do this using the following script.</p><pre>import pandas as pd<br><br>df = pd.read_csv(&#39;YOUR .CSV FILE&#39;)<br>df_cleaned = df.dropna()<br>df_cleaned.to_csv(&#39;cleaned_file.csv&#39;, index=False)<br><br>print(&quot;Lines with missing data have been removed and saved in &#39;cleaned_file.csv&#39;.&quot;)</pre><p>The file is read using <em>“pd.read_csv(‘.csv’)”</em> and the data is saved in the DataFrame “df”. df.dropna()” removes all lines in the DataFrame that contain missing values ‘NaN’. The cleaned DataFrame is then saved in the variable <em>“df_cleaned”</em>. The data is saved in a new .csv file using <em>“df_cleaned.to_csv(‘cleaned_file.csv’, index=False)”</em>. Line indices are not saved. This is followed by an output for the successfully completed process <em>“print(…)”</em>. The code for this dataset cleanup can be found in the file <em>“clean.py”</em> and can also be downloaded later. Next, let’s move on to the actual code of the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LACppaDySWtAn8STFZXotg.gif" /><figcaption>ASCVIT V1 Python Code snippet (GIF by author)</figcaption></figure><h4>LIBRARIES AND MODULES THAT ARE REQUIRED</h4><p>To use the app, various libraries and modules are required, which in combination perform data visualization, statistical analysis and machine learning tasks.</p><pre>import re<br>import subprocess<br><br>import matplotlib.pyplot as plt<br>import numpy as np<br>import pandas as pd<br>import plotly.express as px  <br>import plotly.graph_objects as go  <br>import seaborn as sns<br>from matplotlib.patches import Patch<br>from scipy import stats<br>from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN<br>from sklearn.decomposition import PCA<br>from sklearn.linear_model import LinearRegression, LogisticRegression<br>from statsmodels.stats.multicomp import pairwise_tukeyhsd<br><br>import streamlit as st</pre><p><strong>A note regarding the representation of the diagrams. </strong>Some use <em>“pyplot” </em>(Matplotlib) and others use <em>“plotly”</em> (e.g. Boxplot). Even if the use of <em>“plotly” </em>leads to more interactive graphics, it does not make sense to use it for every type of diagram. Ultimately, the user must decide individually how the diagrams should be displayed. The code must be adapted accordingly.</p><p>The required libraries for the application can be installed using the requirements.txt file in the ZIP directory with the following command.</p><pre>pip install -r requirements.txt</pre><h4>THE DATA OVERVIEW</h4><p>The function <em>“display_data_info()”</em> specifically analyses the Pandas DataFrame <em>“df”</em> and outputs statistical key figures (mean value, standard deviation, etc.) <em>“df.describe()”</em>. The total number of data points (rows) of the DataFrame is output <em>“len(df)”</em>. Likewise, the numerical <em>“numerical_columns” </em>and categorical <em>“categorical_columns” </em>(strings) variables of the DataFrame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*51k7uzslsOyujaC8D0gyBg.png" /><figcaption>ASCVIT V1 App Data Overviews (Image by author)</figcaption></figure><p>The dataset has a total of 64016 data points and 6 numerical and 8 categorical variables. Before you start with certain statistical procedures, you should first look at the data. In the <em>“Data overview” </em>section, you can obtain various information to draw conclusions whether certain tests can be carried out at all.</p><p>For example, if there is no date variable in the dataset, no time series analysis can be carried out. If there is no binary variable, no logistic regression can be carried out. The app is already designed to ask for certain variable categories or to display error messages if incorrect values are transmitted. Next, let’s move on to descriptive statistics.</p><pre>def display_data_info(df):<br>    st.write(&quot;**Data description:**&quot;)<br>    st.write(df.describe())<br>    st.write(f&quot;**Number of data points:** {len(df)}&quot;)<br>    <br>    numerical_columns = df.select_dtypes(include=np.number).columns.tolist()<br>    categorical_columns = df.select_dtypes(include=&#39;object&#39;).columns.tolist()<br><br>    st.write(&quot;**Numerical variables:** &quot;, &quot;, &quot;.join(numerical_columns))<br>    st.write(&quot;**Categorical variables:** &quot;, &quot;, &quot;.join(categorical_columns))<br>    <br>    return numerical_columns, categorical_columns</pre><h4>DESCRIPTIVE STATISTICS</h4><p>The <em>“descriptive_statistics()”</em> function allows the user to select different chart types (histogram, boxplot, pairplot and correlation matrix). A brief explanation of the type follows via <em>“st.markdown(”“”…“”“)”</em>. One or more numerical variables must then be selected “selected_vars”. The option whether logarithmic scaling <em>“apply_log_scale”</em> should be applied is available, except for the correlation matrix. Applying logarithmic scaling to a variable is particularly useful if the data is heavily distorted. The visualization is created using the corresponding diagram function.</p><pre>def descriptive_statistics(df, numerical_columns):<br>    chart_type = st.selectbox(&quot;Select the diagram:&quot;, [&quot;Histogram&quot;, &quot;Boxplot&quot;, &quot;Pairplot&quot;, &quot;Correlation matrix&quot;])<br>    <br>    if chart_type == &quot;Histogram&quot;:<br>        st.markdown(&quot;&quot;&quot;<br>        **Histogram:**<br>        A histogram shows the distribution of a numerical variable. It helps to <br>        recognize how frequently certain values occur in the data and whether there are patterns, such as a normal distribution.<br>        &quot;&quot;&quot;)<br>    elif chart_type == &quot;Boxplot&quot;:<br>        st.markdown(&quot;&quot;&quot;<br>        **Boxplot:**<br>        A boxplot shows the distribution of a numerical variable through its quartiles. <br>        It helps to identify outliers and visualize the dispersion of the data.<br>        &quot;&quot;&quot;)<br>    elif chart_type == &quot;Pairplot&quot;:<br>        st.markdown(&quot;&quot;&quot;<br>        **Pairplot:**<br>        A pairplot shows the relationships between different numerical variables through scatterplots.<br>        It helps to identify possible relationships between variables.<br>        &quot;&quot;&quot;)<br>    elif chart_type == &quot;Correlation matrix&quot;:<br>        st.markdown(&quot;&quot;&quot;<br>        *Correlation matrix:**<br>        The correlation matrix shows the linear relationships between numerical variables.<br>        A positive correlation indicates that high values in one variable also correlate with high values in another.<br>        &quot;&quot;&quot;)<br><br>    if chart_type in [&quot;Pairplot&quot;, &quot;Correlation matrix&quot;]:<br>        selected_vars = st.multiselect(&quot;Select variables:&quot;, numerical_columns, default=numerical_columns)<br>    else:<br>        selected_vars = [st.selectbox(&quot;Select a variable:&quot;, numerical_columns)]<br>    <br>    if chart_type != &quot;Correlation matrix&quot;:<br>        apply_log_scale = st.checkbox(&quot;Apply logarithmic scaling?&quot;, value=False)<br>    else:<br>        apply_log_scale = False <br>    <br>    if st.button(&quot;Create diagram&quot;):<br>        if chart_type == &quot;Histogram&quot;:<br>            plot_histogram(df, selected_vars[0], apply_log_scale)<br>        elif chart_type == &quot;Boxplot&quot;:<br>            plot_boxplot(df, selected_vars[0], apply_log_scale)<br>        elif chart_type == &quot;Pairplot&quot;:<br>            plot_pairplot(df, selected_vars)<br>        elif chart_type == &quot;Correlation matrix&quot;:<br>            plot_correlation_matrix(df, selected_vars)</pre><h4>THE HISTOGRAM FUNCTION</h4><p>The <em>“plot_histogram()”</em> function is used to create the histogram depending on the variable selected by the user. At the beginning, all NaN values are removed from the variable <em>“cleaned_data”</em>. Various statistical key figures (mean value <em>“mean_value”</em>, median <em>“median_value”</em>, standard deviation <em>“std_value”</em>, minimum <em>“min_value”</em> and maximum <em>“max_value” </em>and the upper and lower end of the standard deviation) are calculated.</p><p>Since the data is interpreted by an LLM, as mentioned at the beginning, the dispersion (standard deviation in relation to the range of the data) and distribution (difference between mean and median) of the data is classified. The histogram is created <em>“fix, ax = plt.subplots()”</em>, vertical lines are added to increase the informative value and finally the histogram is displayed <em>“st.pyplot(fig)”</em>. In the case of distorted or exponential data, logarithmic scaling can be activated, whereupon the y-axis of the histogram is adjusted. The graph then looks as follows [3].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kk0lpEvpJet-j7YXzZgVMg.png" /><figcaption>ASCVIT V1 Histogram (pyplot) of critic_score and LLM interpretation (Image by author)</figcaption></figure><p>As there are no models that can read out graphics directly, we create a universal context for the analysis by the LLM. The context contains the results of the statistical calculation and additional instructions or the desired interpretation. This means that the context, which serves as input for the LLM, can be applied to any dataset.</p><p>Specifically, the input includes the aforementioned statistical key figures, an analysis of the distribution (symmetrical, right-skewed or left-skewed), an estimate of the spread (low, moderate or high) and an interpretation formatted for the LLM. Depending on the desired output, the context can be individually adapted and further specified.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gpuc_BO6-Ksh8yhruigt5Q.png" /><figcaption>LLM context example (Image by author)</figcaption></figure><p>The analysis is sent to the LLM <em>“response = query_llm_via_cli(context)”</em>, whereupon an interpretation of the histogram takes place after a short time interval, depending on the performance of the local system <em>“st.write(f”**Histogram Interpretation:** {response}”)”</em>.</p><pre>def plot_histogram(df, variable, apply_log_scale):<br>    cleaned_data = df[variable].dropna()<br><br>    mean_value = cleaned_data.mean()<br>    median_value = cleaned_data.median()<br>    std_value = cleaned_data.std()<br>    min_value = cleaned_data.min()<br>    max_value = cleaned_data.max()<br><br>    std_upper = mean_value + std_value<br>    std_lower = max(0, mean_value - std_value)  <br><br>    concentration_range = (mean_value - std_value, mean_value + std_value)<br><br>    if std_value &lt; (max_value - min_value) / 6:<br>        scatter = &quot;low&quot;<br>    elif std_value &lt; (max_value - min_value) / 3:<br>        scatter = &quot;moderate&quot;<br>    else:<br>        scatter = &quot;high&quot;<br><br>    if abs(mean_value - median_value) &lt; 0.1 * std_value:<br>        distribution = &quot;symmetrical&quot;<br>    elif mean_value &gt; median_value:<br>        distribution = &quot;right-skewed&quot;<br>    else:<br>        distribution = &quot;left-skewed&quot;<br><br>    fig, ax = plt.subplots()<br>    ax.hist(cleaned_data, bins=30, edgecolor=&#39;black&#39;, alpha=0.7)<br><br>    ax.axvline(mean_value, color=&#39;red&#39;, linestyle=&#39;--&#39;, label=f&#39;Mean: {mean_value:.2f}&#39;)<br>    ax.axvline(median_value, color=&#39;green&#39;, linestyle=&#39;-&#39;, label=f&#39;Median: {median_value:.2f}&#39;)<br>    ax.axvline(std_upper, color=&#39;blue&#39;, linestyle=&#39;:&#39;, label=f&#39;+1 Std: {std_upper:.2f}&#39;)<br>    ax.axvline(std_lower, color=&#39;blue&#39;, linestyle=&#39;:&#39;, label=f&#39;-1 Std: {std_lower:.2f}&#39;)<br>    ax.set_title(f&quot;Histogram of {variable}&quot;)<br>    ax.legend(title=f&#39;Std-Deviation: {std_value:.2f}&#39;)<br><br>    if apply_log_scale:<br>        ax.set_yscale(&#39;log&#39;)<br><br>    st.pyplot(fig)<br><br>    context = (<br>        f&quot;Here is an analysis of the distribution of the variable &#39;{variable}&#39;:\n&quot;<br>        f&quot;- Mean: {mean_value:.2f}\n&quot;<br>        f&quot;- Median: {median_value:.2f}\n&quot;<br>        f&quot;- Standard deviation: {std_value:.2f}\n&quot;<br>        f&quot;- Minimum: {min_value:.2f}\n&quot;<br>        f&quot;- Maximum: {max_value:.2f}\n\n&quot;<br>        f&quot;The distribution of the data shows a {distribution} distribution.\n&quot;<br>        f&quot;The small difference between mean and median indicates a {distribution} distribution.\n&quot;<br>        f&quot;A strong concentration of data points is observed between {concentration_range[0]:.2f} and {concentration_range[1]:.2f}.\n&quot;<br>        f&quot;The scatter of the data is described as {scatter}, indicating a relatively tight distribution around the mean.\n\n&quot;<br>        f&quot;Please analyze this distribution in the histogram, paying particular attention to symmetry, scatter, and potential deviations.\n&quot;<br>        f&quot;Avoid calling the distribution normal unless there are explicit indications.\n&quot;<br>        f&quot;Use only the names of the variables {variable} in the analysis!&quot;<br>    )<br><br>    response = query_llm_via_cli(context)<br>    st.write(f&quot;**Histogram Interpretation:** {response}&quot;)</pre><h4>THE BOXPLOT FUNCTION</h4><p>The <em>“plot_boxplot()”</em> function creates a boxplot for a variable selected by the user. Statistical key figures are calculated from the DataFrame based on the variable in order to display the distribution of the data in the diagram and to enable an analysis of the central tendency and dispersion using an LLM. In addition to the mean value, the median and the standard deviation, as with the histogram, the lower <em>“q1”</em>, upper <em>“q3”</em>, the interquartile range <em>“iqr” </em>(Q3 — Q1) and the lower or upper whisker “lower_whisker” and <em>“upper_whisker”</em>, which are based on the interquartile range (1.5 * IQR), are also determined for the boxplot.</p><p>The latter plays a role in identifying outliers and the other parameters where data is above or below a certain value. The boxplot is created using the Plotly library <em>“fig = px.box(df, y=variable)”</em> and finally displayed in the app <em>“st.plotly_chart(fig)”</em>. Logarithmic scaling can also be used for this chart type [4]. The chart then looks as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RfE4ew4ZhSyZv2kCGFr1cw.png" /><figcaption>ASCVIT V1 Boxplot (plotly) of critic_score and LLM interpretation (Image by author)</figcaption></figure><p>As with the histogram, a context is also created for the boxplot, which is forwarded to the LLM. The statistical key figures are transmitted as well as information about potential outliers that lie outside the whisker values. The text sent to the LLM is formatted so that the analysis is performed on these metrics.</p><pre>def plot_boxplot(df, variable, apply_log_scale):<br>    mean_value = df[variable].mean()<br>    median_value = df[variable].median()<br>    std_value = df[variable].std()<br>    q1 = df[variable].quantile(0.25)<br>    q3 = df[variable].quantile(0.75)<br>    iqr = q3 - q1<br>    lower_whisker = max(df[variable].min(), q1 - 1.5 * iqr)<br>    upper_whisker = min(df[variable].max(), q3 + 1.5 * iqr)<br><br>    fig = px.box(df, y=variable)<br>    fig.update_layout(title=f&quot;Boxplot of {variable}&quot;)<br><br>    if apply_log_scale:<br>        fig.update_yaxes(type=&quot;log&quot;)  <br>    <br>    st.plotly_chart(fig)<br><br>    context = (<br>        f&quot;Here is an analysis of the distribution of the variable &#39;{variable}&#39; based on a boxplot:\n&quot;<br>        f&quot;- Mean: {mean_value:.2f}\n&quot;<br>        f&quot;- Median: {median_value:.2f}\n&quot;<br>        f&quot;- Standard deviation: {std_value:.2f}\n&quot;<br>        f&quot;- Lower quartile (Q1): {q1:.2f}\n&quot;<br>        f&quot;- Upper quartile (Q3): {q3:.2f}\n&quot;<br>        f&quot;- Interquartile range (IQR): {iqr:.2f}\n&quot;<br>        f&quot;- Potential outliers outside values from {lower_whisker:.2f} to {upper_whisker:.2f}.\n&quot;<br>        f&quot;Please analyze this distribution and identify patterns or outliers.\n&quot;<br>        f&quot;Use only the names of the variables {variable} in the analysis!&quot;<br>    )<br><br>    response = query_llm_via_cli(context)<br>    st.write(f&quot;**Boxplot Interpretation:** {response}&quot;)</pre><h4>THE PAIRPLOT FUNCTION</h4><p>The <em>“plot_pairplot()”</em> function creates a pairplot based on the variables selected by the user. If less than two variables are selected, an error message is displayed. Scatter plots for all possible combinations of the variables and a linear regression line are displayed to show the relationships between the variables. For this to work, the regression statistics are calculated for all possible pairs of the selected variables using the <em>“calculate_regression_stats” </em>function. The NaN values are removed from the selected variables <em>“selected_vars”</em>.</p><p>A linear regression is performed between the two variables. Here, <em>“var1” </em>is the independent variable x and <em>“var2”</em> is the dependent variable y. The slope and the R2 value <em>“r_squared” </em>are calculated. The results are returned as a list of tuples (var1, var2, slope, r_squared). If three variables are selected [<em>“A”, “B”, “C”</em>], the function will calculate the regression statistics for the pairs (A, B), (A, C), (B, A), (B, C), etc. [5].</p><pre>def calculate_regression_stats(df, selected_vars):<br>    regression_results = []<br>    for var1 in selected_vars:<br>        for var2 in selected_vars:<br>            if var1 != var2:<br>                non_nan_data = df[[var1, var2]].dropna()<br><br>                X = non_nan_data[[var1]].values.reshape(-1, 1)<br>                y = non_nan_data[var2].values<br><br>                if len(X) &gt; 0 and len(y) &gt; 0:<br>                    model = LinearRegression()<br>                    model.fit(X, y)<br>                    r_squared = model.score(X, y)<br>                    slope = model.coef_[0]<br><br>                    regression_results.append((var1, var2, slope, r_squared))<br><br>    return regression_results<br><br>def plot_pairplot(df, selected_vars):<br>    if len(selected_vars) &gt; 1:<br>        st.write(&quot;**Pairplot with regression lines:**&quot;)<br>        pairplot_fig = sns.pairplot(df[selected_vars], kind=&#39;reg&#39;, diag_kind=&#39;kde&#39;, <br>                                    plot_kws={&#39;line_kws&#39;: {&#39;color&#39;: &#39;red&#39;}, &#39;scatter_kws&#39;: {&#39;color&#39;: &#39;blue&#39;}})<br>        st.pyplot(pairplot_fig.fig)<br><br>        corr_matrix = df[selected_vars].corr()<br>        regression_stats = calculate_regression_stats(df, selected_vars)<br>        correlation_list = &quot;\n&quot;.join(<br>            [f&quot;The correlation between {var1} and {var2} is {corr_matrix.at[var1, var2]:.2f}.&quot;<br>             for var1 in corr_matrix.columns for var2 in corr_matrix.columns if var1 != var2]<br>        )<br><br>        regression_list = &quot;\n&quot;.join(<br>            [f&quot;The regression line for {var1} and {var2} has a slope of {slope:.2f} and an R² of {r_squared:.2f}.&quot;<br>             for var1, var2, slope, r_squared in regression_stats]<br>        )<br><br>        context = (<br>            f&quot;Here are the correlation and regression analyses between the selected variables:\n&quot;<br>            f&quot;{correlation_list}\n\n&quot;<br>            f&quot;{regression_list}\n\n&quot;<br>            f&quot;Please analyze these relationships in detail based solely on the numerical values (correlation and regression lines).\n&quot;<br>            f&quot;Use only the names of the variables {selected_vars} in the analysis!&quot;<br>        )<br><br>        response = query_llm_via_cli(context)<br>        st.write(f&quot;**Pairplot Interpretation:** {response}&quot;)<br>    else:<br>        st.error(&quot;At least two variables must be selected for a pairplot.&quot;)</pre><p>KDE (Kernel Density Estimation) is used in the<em> “plot_pairplot()”</em> function for the diagonal to display the distribution of each individual variable. As with the previous functions, a context is again created for the analysis by the LLM. For this type of diagram, the LLM receives the data from the correlation and regression analysis. The text is formatted in such a way that a detailed interpretation of the relationship between the variables is generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c0eroT-7AnJB5wgRQd_26Q.png" /><figcaption>ASCVIT V1 Pairplot (pyplot) of critic_score, na_sales, pal_sales and LLM interpretation (Image by author)</figcaption></figure><h4>THE CORRELATION MATRIX FUNCTION</h4><p>The <em>“plot_correlation_matrix”</em> function is used to create a correlation matrix based on the variables selected by the user <em>“if len(selected_vars) &gt; 1”</em>. If only one variable is selected, an error message is displayed. The visualization is done as a heatmap. The cells of the matrix are colored to show the strength and direction of the correlation. Correlations that are significant are automatically sent to the LLM for further analysis <em>“if var1 != var2 and abs(corr_matrix.at[var1, var2]) &gt;= 0.5”</em>.</p><p>The linear correlation between the selected variables is displayed as the correlation coefficient (value between -1 and +1) <em>“corr_matrix = df[selected_vars].cor()”</em>. With a value of 0, there is no linear correlation. A value close to -1 indicates a strong negative correlation and a value close to +1 indicates a strong positive correlation. The pairs of variables and their correlation values are saved in <em>“high_correlations”</em> [4].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*njH8vAmD7nCD8hJOFqw9Zw.png" /><figcaption>ASCVIT V1 Correlation Matrix (pyplot) with all variables and LLM interpretation (Image by author)</figcaption></figure><p>A context is created for the LLM. The existing significant correlations are classified for the textual description <em>“correlation_list”</em>. A strong correlation (positive or negative) is present with a value greater than 0.7. If the value is between 0.5 and 0.7, there is a moderate correlation and if the value is only just above 0.5, the correlation is weak. If no significant correlations were found, a corresponding message is displayed.</p><pre>def plot_correlation_matrix(df, selected_vars):<br>    if len(selected_vars) &gt; 1:<br>        corr_matrix = df[selected_vars].corr()<br><br>        fig, ax = plt.subplots()<br>        sns.heatmap(corr_matrix, annot=True, cmap=&#39;coolwarm&#39;, ax=ax)<br>        ax.set_title(&quot;Correlation Matrix&quot;)<br>        st.pyplot(fig)<br><br>        high_correlations = []<br>        for var1 in corr_matrix.columns:<br>            for var2 in corr_matrix.columns:<br>                if var1 != var2 and abs(corr_matrix.at[var1, var2]) &gt;= 0.5:  <br>                    if (var2, var1) not in [(v1, v2) for v1, v2, _ in high_correlations]:  <br>                        high_correlations.append((var1, var2, corr_matrix.at[var1, var2]))<br><br>        if high_correlations:<br>            correlation_list = &quot;\n&quot;.join([f&quot;- {var1} and {var2} have a correlation value of {value:.2f}, &quot;<br>                                        f&quot;indicating a {&#39;strong&#39; if abs(value) &gt; 0.7 else &#39;moderate&#39; if abs(value) &gt; 0.5 else &#39;weak&#39;} correlation.&quot;<br>                                        for var1, var2, value in high_correlations])<br><br>            context = (<br>                f&quot;Here is an analysis of the significant correlations between the selected variables in the correlation matrix:\n&quot;<br>                f&quot;{correlation_list}\n\n&quot;<br>                f&quot;Please analyze the correlations solely based on their strength and significance.\n&quot;<br>                f&quot;Use only the names of the variables {selected_vars} in the analysis!&quot;<br>                f&quot;Focus in detail on the statistical relationship and patterns.&quot;<br>            )<br><br>            response = query_llm_via_cli(context)<br>            st.write(f&quot;**Model Response:** {response}&quot;)<br>        else:<br>            st.write(&quot;**No significant correlations were found.**&quot;)<br>    else:<br>        st.write(&quot;**The correlation matrix cannot be displayed because fewer than two variables were selected.**&quot;)</pre><p>In the following statistical procedures, the interpretation of the respective key figures recorded by an LLM is not available. However, based on the previous procedure, independent implementation should not be a problem. Let us now turn to the various hypothesis tests.</p><h4>HYPOTHESIS TESTS SELECTION</h4><p>In the current version, three different tests (t-test, ANOVA and chi-square test) can be carried out <em>“test_type = st.selectbox()”</em>. Depending on the test selected, a brief explanation of its purpose appears. Depending on the area of application, this description can be expanded or removed. The t-test is used to compare the mean values of two groups. The analysis of variance (ANOVA) compares the mean value of more than two groups. The chi-square test tests the independence between two categorical variables. Depending on which test is selected, the corresponding function is performed.</p><h4>T-TEST</h4><p>If the user has opted for the t-test, they must select a group variable (categorical) <em>“group_col”</em> and a value variable (numerical) <em>“value_col”</em>. The group variable defines the two groups to be compared. The value variable compares the mean value between the two groups. Once the selection has been made, the names of the two groups <em>“group1”</em> and <em>“group2”</em> must be entered in a text field <em>“st.text_input()”</em>. The two groups should appear in the selected categorical variable. The logarithmic scaling <em>“apply_log_scale”</em>, which is applied to the value variable, is also available here. When the test is performed, the data of the groups is extracted and the number of data points (after the NaN values have been removed) is output. The t-statistic and the p-value are displayed.</p><p>The first value indicates the difference in the mean values between the two groups relative to the spread of the data. Whether the difference between the groups is statistically significant is indicated by the p-value. If it is less than 0.05, it is significant. To visually highlight the distribution of the two groups <em>“filtered_df = df[df[group_col].isin([group1, group2])]”</em>, a boxplot is created <em>“fig, ax = plt.subplots()”</em>. Here <em>“pyplot”</em> is used, alternatively you can also use <em>“plotly” </em>[6].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aia3I4LkvdYFw5Rn-znHHw.png" /><figcaption>ASCVIT V1 Boxplot with genre (Action/Shooter) and critic_score (Image by author)</figcaption></figure><p>In this example, <em>“genre” </em>was selected as the group variable and <em>“critic_score”</em> as the value variable. Action (group 1) and Shooter (group 2) were defined as groups. The function also calculates whether there are significant outliers in the groups. An outlier is defined as a data point exceeding the upper quartile by more than 1.5 times the interquartile range <em>“outliers_group1/2”</em>. Finally, the outliers found are displayed to confirm the validity of the t-test. If the distortion is too large, this must be taken into account accordingly in order to better classify the reliability and interpretability of the test results.</p><pre>def t_test(df, numerical_columns, categorical_columns):<br>    group_col = st.selectbox(&quot;Choose the group variable:&quot;, categorical_columns)<br>    value_col = st.selectbox(&quot;Choose the value variable:&quot;, numerical_columns)<br>    <br>    group1 = st.text_input(&quot;Name of group 1:&quot;)<br>    group2 = st.text_input(&quot;Name of group 2:&quot;)<br><br>    apply_log_scale = st.checkbox(&quot;Apply logarithmic scaling?&quot;, value=False)<br><br>    if st.button(&quot;Perform t-Test&quot;):<br>        group1_data = df[df[group_col] == group1][value_col]<br>        group2_data = df[df[group_col] == group2][value_col]<br><br>        initial_count_group1 = len(group1_data)<br>        initial_count_group2 = len(group2_data)<br><br>        group1_data = group1_data.dropna()<br>        group2_data = group2_data.dropna()<br><br>        remaining_count_group1 = len(group1_data)<br>        remaining_count_group2 = len(group2_data)<br><br>        st.write(f&quot;**Group 1 ({group1}):** Total number of data points: {initial_count_group1}, without NaN: {remaining_count_group1}&quot;)<br>        st.write(f&quot;**Group 2 ({group2}):** Total number of data points: {initial_count_group2}, without NaN: {remaining_count_group2}&quot;)<br><br>        if apply_log_scale:<br>            group1_data = np.log1p(group1_data)<br>            group2_data = np.log1p(group2_data)<br><br>        if not group1_data.empty and not group2_data.empty:<br><br>            t_stat, p_value = stats.ttest_ind(group1_data, group2_data)<br>            st.markdown(f&quot;**t-Statistic:** {t_stat}&quot;)<br>            st.markdown(f&quot;**p-Value:** {p_value}&quot;)<br>            <br>            filtered_df = df[df[group_col].isin([group1, group2])]<br>            fig, ax = plt.subplots()<br>            sns.boxplot(x=filtered_df[group_col], y=filtered_df[value_col], ax=ax, palette=&quot;Set2&quot;)<br>            ax.set_title(f&quot;Boxplot for {group1} vs. {group2}&quot;)<br><br>            if apply_log_scale:<br>                ax.set_yscale(&#39;log&#39;)<br><br>            st.pyplot(fig)<br><br>            outliers_group1 = group1_data[group1_data &gt; group1_data.quantile(0.75) + 1.5 * (group1_data.quantile(0.75) - group1_data.quantile(0.25))]<br>            outliers_group2 = group2_data[group2_data &gt; group2_data.quantile(0.75) + 1.5 * (group2_data.quantile(0.75) - group2_data.quantile(0.25))]<br><br>            st.write(&quot;**Outlier Analysis:**&quot;)<br>            if not outliers_group1.empty:<br>                st.write(f&quot;In group 1 ({group1}) there are {len(outliers_group1)} outliers.&quot;)<br>            else:<br>                st.write(f&quot;In group 1 ({group1}) there are no significant outliers.&quot;)<br><br>            if not outliers_group2.empty:<br>                st.write(f&quot;In group 2 ({group2}) there are {len(outliers_group2)} outliers.&quot;)<br>            else:<br>                st.write(f&quot;In group 2 ({group2}) there are no significant outliers.&quot;)<br>        else:<br>            st.error(&quot;One or both groups contain no data after removing NaN values.&quot;)</pre><h4><strong>ANOVA-TEST</strong></h4><p>The <em>“anova_test()”</em> function integrates the option of performing an ANOVA test. This test checks whether there are significant differences in the mean values of several groups. The data is first cleaned <em>“df_clean”</em>. If the ANOVA test is significant, a Tukey’s HSD test (Honestly Significant Difference) is also carried out. At the beginning, a group variable and a value variable are again defined. If a group has fewer than 2 data points, it is excluded <em>“valid_groups = group_sizes[group_sizes &gt;= 2].index”</em>.</p><p>If less than two groups remain after the adjustment, an error message is displayed and the test is not performed. The ANOVA test calculates the F-value and the p-value. The variability between the groups compared to the variability within the group is measured by the F-value. Whether the difference between the mean values of the group is significant is indicated by the p-value. If the value is less than 0.05, at least one group is significantly different. To visualize the results, a boxplot is created using <em>“pyplot”</em> [7].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3LWaibBGnyCw9ghWI-gP-Q.png" /><figcaption>ASCVIT V1 Boxplot with console and critic_score (Image by author)</figcaption></figure><p>If the ANOVA test produces significant results, a Tukey’s test is carried out to examine the differences between the individual group pairs in concrete terms. The ANOVA test therefore does not show which groups differ from each other. A diagram is created that shows the pairwise mean differences between the groups and their confidence interval <em>“st.pyplot(tukey.plot_simultaneous())”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PI8EkWKMZG3wQGu_5vkLbw.png" /><figcaption>ASCVIT V1 Result of the Tukey Test (Image by author)</figcaption></figure><p>Below the diagram, the results are displayed in a table <em>“st.dataframe(tukey_results_df, height=400)”</em>. The table contains the two groups, the mean difference <em>“meandiff”</em>, the adjusted p-value <em>“p-adj”</em>, the confidence interval and whether the null hypothesis can be rejected or not <em>“reject” </em>(True = significant, Fals = not significant). A brief example of the confidence interval. For the 3DS and GBA consoles, the interval lies between -0.9319 and -0.0061 and is therefore completely below zero. The difference in the mean value is significant.</p><p>The key figures can be used to have the results interpreted by an LLM. There is also an option to download the data as a .csv file for further statistical analyses (e.g. regression analyses) [7].</p><pre>def anova_test(df, numerical_columns, categorical_columns):<br>    group_col = st.selectbox(&quot;Choose the group variable:&quot;, categorical_columns)<br>    value_col = st.selectbox(&quot;Choose the value variable:&quot;, numerical_columns)<br><br>    if st.button(&quot;Perform ANOVA&quot;):<br>        df_clean = df[[group_col, value_col]].dropna()<br><br>        group_sizes = df_clean.groupby(group_col).size()<br>        valid_groups = group_sizes[group_sizes &gt;= 2].index<br>        df_filtered = df_clean[df_clean[group_col].isin(valid_groups)]<br><br>        if len(valid_groups) &lt; 2:<br>            st.error(&quot;After removing small groups, there are not enough groups left for the ANOVA test.&quot;)<br>        else:<br>            grouped_data = [group[value_col].values for name, group in df_filtered.groupby(group_col)]<br>            try:<br>                anova_result = stats.f_oneway(*grouped_data)<br>                st.markdown(f&quot;**F-Value:** {anova_result.statistic}&quot;)<br>                st.markdown(f&quot;**p-Value:** {anova_result.pvalue}&quot;)<br><br>                fig, ax = plt.subplots(figsize=(10, 6))<br>                sns.boxplot(x=group_col, y=value_col, data=df_filtered, ax=ax)<br>                plt.xticks(rotation=90)<br>                st.pyplot(fig)<br><br>                if anova_result.pvalue &lt; 0.05:<br>                    st.write(&quot;The ANOVA test is significant. Tukey&#39;s HSD test will be performed.&quot;)<br>                    try:<br>                        tukey = pairwise_tukeyhsd(endog=df_filtered[value_col], groups=df_filtered[group_col], alpha=0.05)<br>                        st.pyplot(tukey.plot_simultaneous())<br><br>                        tukey_results_df = pd.DataFrame(data=tukey.summary().data[1:], columns=tukey.summary().data[0])<br>                        st.write(&quot;Results of the Tukey HSD test:&quot;)<br>                        st.dataframe(tukey_results_df, height=400)<br><br>                        csv = tukey_results_df.to_csv(index=False)<br>                        st.download_button(label=&quot;Download Tukey HSD results as CSV&quot;, data=csv, file_name=&#39;tukey_hsd_results.csv&#39;, mime=&#39;text/csv&#39;)<br><br>                    except Exception as e:<br>                        st.error(f&quot;An error occurred during Tukey&#39;s HSD test: {str(e)}&quot;)<br><br>            except ValueError as e:<br>                st.error(f&quot;An error occurred: {str(e)}.&quot;)</pre><h4><strong>CHI-QUADRAT-TEST</strong></h4><p>The <em>“chi_square_test()”</em> function checks whether there is a statistically significant relationship between two categorical variables. As only categorical variables can be used, the option to activate logarithmic scaling is not required. Specifically, whether the frequency of observations in the categories is independent of each other or whether there is a correlation. The user selects two of the existing categorical variables. The NaN values are removed and only the top 10 most frequent categories are selected for each variable in order to keep the analysis manageable <em>“value_counts().nlargest(10).index”</em>.</p><p>A crosstab <em>“contingency_table”</em> is created, which shows the frequencies of the combinations of categories in the two selected variables using a heatmap. If the crosstab is not valid (too little data or only one category), the test is not performed [8].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bQX0gv5D4kyfo4-ovZsAoA.png" /><figcaption>ASCVIT V1 Heatmap with genre and console (Image by author)</figcaption></figure><p>The test calculates various values. The chi-square value <em>“chi2”</em> determines the measure of the difference between the observed and expected frequencies. A strong difference is present with high values. As with the other analyses, the p-value<em> “p”</em> shows whether the difference is significant. The number of degrees of freedom of the test <em>“dof”</em> is indicated as well as the expected frequencies <em>“expected”</em>.</p><pre>def chi_square_test(df, categorical_columns):<br>    cat_var1 = st.selectbox(&quot;Choose the first group variable:&quot;, categorical_columns)<br>    cat_var2 = st.selectbox(&quot;Choose the second group variable:&quot;, categorical_columns)<br>    <br>    if st.button(&quot;Perform Chi-square test&quot;):<br>        df_clean = df[[cat_var1, cat_var2]].dropna()<br><br>        top_cat_var1 = df_clean[cat_var1].value_counts().nlargest(10).index<br>        top_cat_var2 = df_clean[cat_var2].value_counts().nlargest(10).index<br>        df_filtered = df_clean[df_clean[cat_var1].isin(top_cat_var1) &amp; df_clean[cat_var2].isin(top_cat_var2)]<br><br>        try:<br>            contingency_table = pd.crosstab(df_filtered[cat_var1], df_filtered[cat_var2])<br><br>            if contingency_table.empty or contingency_table.shape[0] &lt; 2 or contingency_table.shape[1] &lt; 2:<br>                st.error(&quot;The contingency table is invalid. Check the variables.&quot;)<br>            else:<br>                chi2, p, dof, expected = stats.chi2_contingency(contingency_table)<br>                st.markdown(f&quot;**Chi-square:** {chi2}&quot;)<br>                st.markdown(f&quot;**p-Value:** {p}&quot;)<br>                <br>                st.write(&quot;**Heatmap of the contingency table:**&quot;)<br>                fig, ax = plt.subplots(figsize=(12, 10))  # Larger display<br>                sns.heatmap(contingency_table, annot=False, cmap=&quot;YlGnBu&quot;, ax=ax)<br>                ax.set_title(f&quot;Heatmap of the contingency table: {cat_var1} vs. {cat_var2} top 10&quot;)<br>                plt.xticks(rotation=90)<br>                st.pyplot(fig)<br><br>        except ValueError as e:<br>            st.error(f&quot;An error occurred: {str(e)}.&quot;)</pre><h4>REGRESSION ANALYSIS SELECTION</h4><p>The selection of the available regression analyses (linear, logistic and multivariate) is similar to the selection of the various hypothesis tests. Once an analysis method has been selected, a brief explanation is displayed and the corresponding function is called up.</p><h4>LINEAR REGRESSION ANALYSIS</h4><p>At the beginning of the <em>“linear_regression()”</em> function, a correlation matrix is created from all available numerical variables in the uploaded dataset <em>“corr_matrix = df[numerical_columns].corr()”</em>. The matrix is intended to help the user understand the relationships between the variables in order to identify the variables for which regression analyses are appropriate and those for which they are not (multicollinearity).</p><p>Finally, the dependent variable and one or more independent variables are selected. The data is cleaned and a linear regression model is created for all selected independent variables <em>“model = LinearRegression()”</em>. The regression coefficient and the intercept are specified. After the overall model has been run, a separate linear regression model is created for each independent variable and represented by a scatterplot [9].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yq-WoSQxZ6wyjWnRPR6zvQ.png" /><figcaption>ASCVIT V1 Lineare Regression pal_sales, na_sales and total_sales (Image by author)</figcaption></figure><p>The regression coefficient shown indicates the extent to which the dependent variable is changed when the respective independent variable is changed by one unit. Assuming that all other variables remain constant. The value that the dependent variable assumes when all independent variables are zero is indicated by the intercept.</p><pre>def linear_regression(df, numerical_columns):<br>    st.write(&quot;**Correlation matrix of numerical variables:**&quot;)<br>    corr_matrix = df[numerical_columns].corr()<br>    fig, ax = plt.subplots(figsize=(10, 8))<br>    sns.heatmap(corr_matrix, annot=True, cmap=&#39;coolwarm&#39;, ax=ax)<br>    st.pyplot(fig)<br><br>    dependent_var = st.selectbox(&quot;Choose the dependent variable:&quot;, numerical_columns)<br>    independent_vars = st.multiselect(&quot;Choose the independent variables:&quot;, numerical_columns)<br><br>    if independent_vars:<br>        if st.button(&quot;Perform regression&quot;):<br>            X = df[independent_vars].dropna()<br>            y = df[dependent_var].loc[X.index]<br>            y = y.dropna()<br>            X = X.loc[y.index]<br><br>            if y.isnull().values.any():<br>                st.error(&quot;The dependent variable still contains missing values. Please clean the data.&quot;)<br>            else:<br>                model = LinearRegression()<br>                model.fit(X, y)<br><br>                st.markdown(&quot;**Regression coefficients:**&quot;)<br>                for var, coef in zip(independent_vars, model.coef_):<br>                    st.write(f&quot;- {var}: {coef}&quot;)<br>                st.write(f&quot;**Intercept:** {model.intercept_}&quot;)<br><br>                for var in independent_vars:<br>                    X_single_var = X[[var]]  # Use only the current independent variable<br>                    model_single = LinearRegression()<br>                    model_single.fit(X_single_var, y)<br><br>                    fig, ax = plt.subplots()<br>                    ax.scatter(X[var], y, edgecolor=&#39;none&#39;, facecolors=&#39;blue&#39;, s=5, label=&#39;Data points&#39;)<br><br>                    ax.plot(X[var], model_single.predict(X_single_var), color=&#39;red&#39;, label=&#39;Regression line&#39;)<br>                    ax.set_xlabel(var)<br>                    ax.set_ylabel(dependent_var)<br>                    ax.set_title(f&quot;{dependent_var} vs {var}&quot;)<br>                    ax.legend()<br>                    st.pyplot(fig)</pre><h4>THE LOGISTIC REGRESSION ANALYSIS</h4><p>As with the other functions, the user selects dependent and independent variables at the beginning. In this analysis method, the dependent variable must be binary (0/1). To demonstrate the function, I have created some data that has nothing to do with the dataset used so far. Alternatively, you can also manually adjust the values of variables, as long as there are not too many categories. If an incorrect variable is selected, a corresponding error message is displayed.</p><p>If everything is defined correctly, the logistic regression is performed and the model uses the independent variables to model the probability for the target variable. Specifically, this is the probability that an event will occur. The coefficients are specified for each independent variable and the logistic function is visualized. This shows how the probability of the target result (1 instead of 0) changes when the independent variable changes [10].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lgUKiY7AlPpkxPxw7uVX-A.png" /><figcaption>ASCVIT V1 Logistic regression with fictitious values ​​for demonstration purposes (Image by author)</figcaption></figure><p>In the scatterplot, the red line represents the prediction probability for the target result, i.e. the probability that result 1 will occur. The independent variable varies. The <em>“logistic_regression()”</em> function is very well suited to binary classification problems where you want to predict the occurrence of an event based on several factors.</p><pre>def logistic_regression(df, numerical_columns):<br>    dependent_var = st.selectbox(&quot;Choose the dependent variable (binary):&quot;, numerical_columns)<br>    independent_vars = st.multiselect(&quot;Choose the independent variables:&quot;, numerical_columns)<br><br>    if independent_vars:<br>        if st.button(&quot;Perform logistic regression&quot;):<br>            X = df[independent_vars].dropna()<br>            y = df[dependent_var].loc[X.index].dropna()<br>            X = X.loc[y.index]<br><br>            unique_values = y.unique()<br>            if len(unique_values) != 2:<br>                st.error(&quot;The dependent variable must be binary (e.g., 0 and 1).&quot;)<br>            else:<br>                model = LogisticRegression()<br>                model.fit(X, y)<br><br>                st.write(&quot;**Logistic regression coefficients:**&quot;)<br>                for var, coef in zip(independent_vars, model.coef_[0]):<br>                    st.write(f&quot;- {var}: {coef}&quot;)<br>                st.write(f&quot;**Intercept:** {model.intercept_[0]}&quot;)<br><br>                for var in independent_vars:<br>                    fig, ax = plt.subplots()<br><br>                    ax.scatter(X[var], y, label=&#39;Data points&#39;)<br>                    x_range = np.linspace(X[var].min(), X[var].max(), 300).reshape(-1, 1)<br>                    X_copy = pd.DataFrame(np.tile(X.mean().values, (300, 1)), columns=X.columns)<br>                    X_copy[var] = x_range.flatten()  # Vary the current variable var<br>                    y_prob = model.predict_proba(X_copy)[:, 1]<br><br>                    ax.plot(x_range, y_prob, color=&#39;red&#39;, label=&#39;Logistic function&#39;)<br>                    ax.set_xlabel(var)<br>                    ax.set_ylabel(f&#39;Probability ({dependent_var})&#39;)<br>                    ax.set_title(f&#39;Logistic regression: {dependent_var} vs {var}&#39;)<br>                    ax.legend()<br>                    st.pyplot(fig)</pre><h4>MULTIVARIATE REGRESSION ANALYSIS</h4><p>In multivariate regression analysis, the user must select several dependent variables and one or more independent variables. The analysis examines how the dependent variables are influenced by the independent variables. After the variable selection, the NaN values are removed again and an error message is displayed if necessary. The model outputs the regression coefficient and the intercept for all dependent variables.</p><p>A scatterplot with regression line is created for all combinations of independent and dependent variables. This function makes it possible to analyze several target variables simultaneously and to model their relationship to several predictors [11].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oRVHbn5N3_yL1vECbZEpgw.png" /><figcaption>ASCVIT V1 Multivariate regression (plotly) example (Image by author)</figcaption></figure><pre>def multivariate_regression(df, numerical_columns):<br>    dependent_vars = st.multiselect(&quot;**Choose the dependent variables (multiple):**&quot;, numerical_columns)<br>    independent_vars = st.multiselect(&quot;**Choose the independent variables:**&quot;, numerical_columns)<br><br>    if dependent_vars and independent_vars:<br>        if st.button(&quot;Perform multivariate regression&quot;):<br>            X = df[independent_vars].dropna()<br>            Y = df[dependent_vars].loc[X.index].dropna()<br>            X = X.loc[Y.index]<br><br>            if X.shape[1] != len(independent_vars) or Y.shape[1] != len(dependent_vars):<br>                st.error(&quot;The number of independent or dependent variables does not match.&quot;)<br>                return<br><br>            model = LinearRegression()<br>            model.fit(X, Y)<br><br>            st.write(&quot;**Multivariate regression coefficients:**&quot;)<br>            for i, dep_var in enumerate(dependent_vars):<br>                st.write(f&quot;\nFor the dependent variable: **{dep_var}**&quot;)<br>                st.write(f&quot;Intercept: {model.intercept_[i]}&quot;)<br>                for var, coef in zip(independent_vars, model.coef_[i]):<br>                    st.write(f&quot;- {var}: {coef}&quot;)<br><br>            for dep_var in dependent_vars:<br>                for var in independent_vars:<br>                    fig, ax = plt.subplots()<br>                    ax.scatter(X[var], Y[dep_var], label=&#39;Data points&#39;)<br><br>                    x_range = np.linspace(X[var].min(), X[var].max(), 300).reshape(-1, 1)<br>                    X_copy = pd.DataFrame(np.tile(X.mean().values, (300, 1)), columns=X.columns)<br>                    X_copy[var] = x_range.flatten()<br><br>                    y_pred = model.predict(X_copy)<br><br>                    ax.plot(x_range, y_pred[:, dependent_vars.index(dep_var)], color=&#39;red&#39;, label=&#39;Regression line&#39;)<br>                    ax.set_xlabel(var)<br>                    ax.set_ylabel(dep_var)<br>                    ax.set_title(f&#39;Multivariate regression: {dep_var} vs {var}&#39;)<br>                    ax.legend()<br>                    st.plotly_chart(fig)</pre><h4>THE TIME SERIES ANALYSIS</h4><p>This analysis method performs an analysis over time. For this purpose, a given time series is grouped by year and the annual average of a value is calculated and displayed. A time variable is required for the analysis; if the dataset used does not contain one, it cannot be performed. In the dataset I have selected, there is the variable <em>“release_date”</em>, which contains the release date of the respective game.</p><p>The selected time variable is converted to the date format <em>“df[time_var]”</em>. If the data points are invalid, they are converted to NaN values and removed <em>“df = df.dropna(subset=[time_var])”</em>. The data is then grouped by year <em>“df[‘year’] = df[time_var].dt.year”</em> and the annual average for the specified value variable “value_var” is calculated <em>“yearly_avg”</em>. The minimum and maximum annual average values of the value variable are calculated as well as the overall average across all data points <em>“overall_avg”</em>. The annual average of the value variable per year is then displayed via a line chart. The overall average is integrated on the horizontal line. The values are displayed alternately above and below the data points to improve readability [12].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XRsNEWsxZastRklmmSWPvQ.png" /><figcaption>ASCVIT V1 Time series analysis with year and critic_score (Image by author)</figcaption></figure><p>Important statistical key figures are displayed below the diagram, which can be easily interpreted as in the descriptive analysis using an LLM. Specifically, the standard deviation, the variance and the minimum and maximum of the value variable as well as the year are displayed. The <em>“perform_time_series_analysis()”</em> function is suitable for analyzing time trends in a data series. This allows an initial analysis of the variability over time.</p><pre>def perform_time_series_analysis(df, time_var, value_var):<br>    df[time_var] = pd.to_datetime(df[time_var], errors=&#39;coerce&#39;)<br>    df = df.dropna(subset=[time_var])<br><br>    if df.empty:<br>        st.error(&quot;**Error:** The time variable has an incorrect format.&quot;)<br>    else:<br>        df[&#39;year&#39;] = df[time_var].dt.year<br>        yearly_avg = df.groupby(&#39;year&#39;)[value_var].mean().reset_index()<br><br>        y_min = yearly_avg[value_var].min()<br>        y_max = yearly_avg[value_var].max()<br>        y_range = y_max - y_min<br>        y_buffer = y_range * 0.05<br><br>        overall_avg = df[value_var].mean()<br><br>        fig, ax = plt.subplots(figsize=(10, 6))<br>        ax.plot(yearly_avg[&#39;year&#39;], yearly_avg[value_var], marker=&#39;o&#39;, label=&#39;Yearly average&#39;)<br>        ax.axhline(overall_avg, color=&#39;red&#39;, linestyle=&#39;--&#39;, label=f&#39;Overall average: {overall_avg:.2f}&#39;)<br>        ax.set_title(f&#39;Average {value_var} per year&#39;)<br>        ax.set_xlabel(&#39;Year&#39;)<br>        ax.set_ylabel(f&#39;Average {value_var}&#39;)<br>        ax.set_ylim(y_min - y_buffer, y_max + y_buffer)<br><br>        ax.text(yearly_avg[&#39;year&#39;].max() - (yearly_avg[&#39;year&#39;].max() - yearly_avg[&#39;year&#39;].min()) * 0.05, <br>                overall_avg + y_buffer, <br>                f&#39;{overall_avg:.2f}&#39;, color=&#39;red&#39;, ha=&#39;right&#39;, va=&#39;center&#39;)<br><br>        for i in range(len(yearly_avg)):<br>            if i % 2 == 0:<br>                ax.text(yearly_avg[&#39;year&#39;][i], yearly_avg[value_var][i] + y_buffer/2, <br>                        f&#39;{yearly_avg[value_var][i]:.2f}&#39;, color=&#39;blue&#39;, ha=&#39;center&#39;, va=&#39;bottom&#39;)<br>            else:<br>                ax.text(yearly_avg[&#39;year&#39;][i], yearly_avg[value_var][i] - y_buffer/2, <br>                        f&#39;{yearly_avg[value_var][i]:.2f}&#39;, color=&#39;blue&#39;, ha=&#39;center&#39;, va=&#39;top&#39;)<br><br>        plt.xticks(rotation=45)<br>        ax.legend()<br>        st.pyplot(fig)<br><br>        st.write(f&quot;**Standard deviation:** {df[value_var].std():.2f}&quot;)<br>        st.write(f&quot;**Variance:** {df[value_var].var():.2f}&quot;)<br>        st.write(f&quot;**Minimum {value_var}:** {y_min:.2f} in year {yearly_avg.loc[yearly_avg[value_var].idxmin(), &#39;year&#39;]}&quot;)<br>        st.write(f&quot;**Maximum {value_var}:** {y_max:.2f} in year {yearly_avg.loc[yearly_avg[value_var].idxmax(), &#39;year&#39;]}&quot;)</pre><h4>CLUSTERING METHOD SELECTION</h4><p>As with the hypothesis tests and regression analyses, there are also various options to choose from in the area of clustering methods. The selection function has a similar structure to the others. A method is selected and the corresponding function is executed. A brief explanation of the method is also shown here. Depending on the method, the number of clusters must be defined. For k-Means and hierarchical clustering, a maximum of 10 clusters can be defined. For DBSCAN, the radius <em>“eps”</em> and the minimum number of points per cluster <em>“min_samples”</em> are queried. At least two numerical variables must be selected for each method.</p><h4>k-MEANS CLUSTERING</h4><p>The k-Means algorithm divides the data into <em>“n_clusters”</em>. The points are grouped in such a way that the distance between the points within the cluster is minimized. The number of clusters is determined by the user. Depending on the number, the algorithm calculates which data points belong to which cluster. The result is sent to the <em>“visualize_clusters()” </em>function for visualization [13].</p><pre>def perform_kmeans(X, n_clusters):<br>    kmeans = KMeans(n_clusters=n_clusters, random_state=42)<br>    X[&#39;Cluster&#39;] = kmeans.fit_predict(X)<br>    <br>    visualize_clusters(X, &#39;k-Means Clustering&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1fpTg_4qPoTTqsiQFv3blw.png" /><figcaption>ASCVIT V1 k-Means Clustering with PCA reduction (Image by author)</figcaption></figure><h4>HIERARCHICAL CLUSTERING</h4><p>A hierarchy of clusters is created here, whereby agglomerative or divisive methods can be used. Agglomerative clustering is used in the function, whereby each data point is initially considered as a separate cluster before they are successively merged. The number of clusters is determined by the user and the algorithm divides the data according to the number. The same function as for k-Means is used for visualization <em>“visualize_clusters(X, ‘Hierarchical Clustering’)”</em> [14].</p><pre>def perform_hierarchical_clustering(X, n_clusters):<br>    hierarchical_clustering = AgglomerativeClustering(n_clusters=n_clusters)<br>    X[&#39;Cluster&#39;] = hierarchical_clustering.fit_predict(X)<br>       <br>    visualize_clusters(X, &#39;Hierarchical Clustering&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ey2xh0wvOSyqiulAYv23Nw.png" /><figcaption>ASCVIT V1 Hierarchical Clustering with PCA reduction (Image by author)</figcaption></figure><h4>DBSCAN CLUSTERING</h4><p>With this method, data points are grouped based on the density of their surroundings. The method is well suited to detecting outliers (noise) and finding clusters of any shape. Here, the user does not specify the number of clusters, but the maximum distance <em>“eps”</em> between two points before they are considered neighbors. The minimum number of points that occur in a cluster <em>“min_samples”</em> is also defined. The visualization is also created by <em>“visualize_clusters()”</em> [15].</p><pre>def perform_dbscan(X, eps, min_samples):<br>    dbscan = DBSCAN(eps=eps, min_samples=min_samples)<br>    X[&#39;Cluster&#39;] = dbscan.fit_predict(X)<br>    <br>    visualize_clusters(X, &#39;DBSCAN Clustering&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NrQtXC679v-7UkBkoy0olw.png" /><figcaption>ASCVIT V1 DBSCAN Clustering with PCA reduction (Image by author)</figcaption></figure><h4>CLUSTER VISUALIZATION</h4><p>The results of the three different clustering methods are visualized in the <em>“visualize_clusters”</em> function using Principal Component Analysis (PCA). The dimensions of the data are reduced by PCA to two components <em>“n_components”</em> in order to be able to display the clusters. It is checked whether there are enough data points and variables <em>“num_samples”</em>; if this is not the case, an error message is displayed. The clusters are visualized by a scatterplot that shows the data points in the first two PCA components.</p><p>The clusters are displayed in different colors <em>“cmap=‘tab10’”</em>. In the diagrams, the axis label <em>“ax.set_x/ylabel”</em> and the legend <em>“legend_labels”</em> are adapted for better interpretation. The size of the data points “s” and the transparency <em>“alpha”</em> have also been adjusted to improve visibility. Outliers are automatically assigned to cluster -1 in DBSCAN. A table with the average values of the variable for each cluster is displayed below the visualization <em>“st.dataframe(cluster_means)”</em>.</p><pre>def visualize_clusters(X, title):<br>    num_samples, num_features = X.shape<br>    <br>    n_components = min(num_samples, num_features, 2) <br><br>    if n_components &lt; 2:<br>        st.error(&quot;Not enough data points or variables to perform PCA.&quot;)<br>        return<br><br>    pca = PCA(n_components=n_components)<br>    <br>    try:<br>        X_pca = pca.fit_transform(X.drop(columns=[&#39;Cluster&#39;]))<br>        <br>        fig, ax = plt.subplots(figsize=(10, 6))<br>        scatter = ax.scatter(X_pca[:, 0], X_pca[:, 1], c=X[&#39;Cluster&#39;], cmap=&#39;tab10&#39;, s=25, alpha=0.4)<br>        <br>        ax.set_title(title)<br>        ax.set_xlabel(f&#39;PCA 1&#39; if n_components &gt;= 1 else &#39;&#39;)<br>        ax.set_ylabel(f&#39;PCA 2&#39; if n_components == 2 else &#39;&#39;)<br><br>        cluster_counts = X[&#39;Cluster&#39;].value_counts()  <br><br>        legend_labels = [f&quot;Cluster {int(cluster)} ({count} points)&quot; for cluster, count in cluster_counts.items()]<br>        legend1 = ax.legend(handles=scatter.legend_elements()[0], labels=legend_labels)<br>        ax.add_artist(legend1)<br><br>        st.pyplot(fig)<br><br>        st.write(f&quot;**Average values per cluster:**&quot;)<br>        cluster_means = X.groupby(&#39;Cluster&#39;).mean()<br>        st.dataframe(cluster_means)<br><br>    except ValueError as e:<br>        st.error(f&quot;**Error:** Not enough variables were selected.&quot;)</pre><h4>COMMUNICATION WITH THE LLM</h4><p>In this first version of the app, the output of the statistical calculations is only analyzed in the descriptive area. The key figures are interpreted using the <em>“query_llm_via_cli”</em> function. Specifically, the function is used to communicate with the LLM via a command line (CLI). To achieve this, the Python module <em>“subprocess”</em> is used to start the process via the command line. The LLM is started via the command [<em>“ollama”, “run”, “llama3.1”</em>]. The input is stored in <em>“stdin”</em>, the output in<em> “stout”</em>.</p><p>Errors and warnings are stored in <em>“stderr”</em>, which hopefully do not occur. The input is sent to the model via <em>“process.communicate”</em>. Specifically, the created <em>“context”</em> is sent to the function to communicate with the LLM. If there is no response from the model, a timeout mechanism <em>“timeout=40”</em> is included, which stops the execution after 40 seconds. Depending on the computing power of the system used, a response from the model should be displayed much earlier. The model’s response is cleaned up and passed to <em>“extract_relevant_answer”</em> in order to extract relevant information [1].</p><pre>def query_llm_via_cli(input_text):<br>    &quot;&quot;&quot;Sends the question and context to the LLM and receives a response&quot;&quot;&quot;<br>    try:<br>        process = subprocess.Popen(<br>            [&quot;ollama&quot;, &quot;run&quot;, &quot;llama3.1&quot;],<br>            stdin=subprocess.PIPE,<br>            stdout=subprocess.PIPE,<br>            stderr=subprocess.PIPE,<br>            text=True,<br>            encoding=&#39;utf-8&#39;,<br>            errors=&#39;ignore&#39;,<br>            bufsize=1<br>        )<br>        stdout, stderr = process.communicate(input=f&quot;{input_text}\n&quot;, timeout=40)<br><br>        if process.returncode != 0:<br>            return f&quot;Error in the model request: {stderr.strip()}&quot;<br><br>        response = re.sub(r&#39;\x1b\[.*?m&#39;, &#39;&#39;, stdout)<br>        return extract_relevant_answer(response)<br><br>    except subprocess.TimeoutExpired:<br>        process.kill()<br>        return &quot;Timeout for the model request&quot;<br>    except Exception as e:<br>        return f&quot;An unexpected error has occurred: {str(e)}&quot;<br><br>def extract_relevant_answer(full_response):<br>    response_lines = full_response.splitlines()<br>    if response_lines:<br>        return &quot;\n&quot;.join(response_lines).strip()<br>    return &quot;No answer received&quot;</pre><h4>MAIN FUNCTION OF THE APP</h4><p>The structure of the app is defined by the <em>“main()”</em> function. The title is set <em>“st.title()”</em> and the sidebar for uploading the dataset in CSV or Excel format <em>“uploaded_file”</em> is submitted. Once a file has been uploaded, it is analyzed and the numerical and categorical variables are extracted. Here and in many other situations, <em>“session_state”</em> is used by Streamlit to store certain parameters that are relevant for the selection in the analysis methods.</p><p>The variables <em>“numerical_columns”</em> and <em>“categorical_columns”</em> are updated as soon as a new dataset is uploaded. Once data is available, the user can select from the various analysis methods. Once a method has been selected, it is displayed and can be carried out after the corresponding variables have been defined. The main function controls the interactive statistical analysis of the app.</p><h4>CUSTOMIZATION OPTIONS</h4><p>As already mentioned, the app can be expanded to include other analysis methods due to the modular structure of the code. The functionality of interpreting statistical key figures using an LLM can also be transferred to other methods. Llama3.1 (8B) from Meta is currently used, but another LLM (e.g. Mistral) from Ollama can also be used. The command in the <em>“query_llm_via_cli”</em> function must then be adapted accordingly.</p><p>Depending on the available power, models with more parameters can also be used. The design of the diagrams can be further refined, as can the transmitted contexts, in order to improve the output of the LLM. Alternatively, you can also create a new model file to adjust certain parameters (e.g. parameters) of the LLM and thereby improve the interpretation of the data.</p><h4>ASCVIT V1 PYTHON SCRIPT [GITHUB]</h4><p>The app code can be downloaded from the following <a href="https://github.com/stefanpietrusky/ASCVITV1"><strong>GitHub repository</strong></a>. The app is started in the corresponding directory using the following command:</p><pre>Streamlit run app.py</pre><h4>CONCLUSION</h4><p>In this article, I showed how Streamlit can be used to create an app that can be used to analyze datasets using various methods. I also showed how an interpretation can be integrated into the app using an LLM, which results in real added value. Data is not only automatically visualized and statistical parameters are output, but also classified. The application offers a lot of potential for further development. I have listed some suggestions in the penultimate section. Have fun using and customizing the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*j6DnTo7-FNuMgfQOD6HrGA.gif" /><figcaption>You can clap up to 50 times!</figcaption></figure><p>[1] Pietrusky, S. (2024, August 21). <em>How to talk to a PDF file without using proprietary models: CLI, Streamlit, Ollama.</em> <em>Towards Data Science</em>. <a href="https://medium.com/towards-data-science/how-to-talk-to-a-pdf-file-without-using-proprietary-models-cli-streamlit-ollama-6c22437ed932">URL</a></p><p>[2] Maven Analytics. (2024, Juni 10.). <em>Data Playground, Video Game Sales.</em> <a href="https://mavenanalytics.io/data-playground?accessType=open&amp;order=date_added%2Cdesc&amp;search=game">URL</a></p><p>[3] Hastie, T., Tibshirani, R., &amp; Friedman, J. (2009). <em>The elements of statistical learning: Data mining, inference, and prediction</em> (2nd ed.). Stanford University. <a href="https://hastie.su.domains/Papers/ESLII.pdf">URL</a></p><p>[4] Bruce, P., Bruce, A., &amp; Gedeck, P. (2021). <em>Praktische Statistik für Data Scientists: 50+ essenzielle Konzepte mit R und Python</em><strong> </strong>(2nd ed.). O’Reilly.</p><p>[5] VanderPlas J. (2017). <em>Python Data Science Handbook: Essential Tools for Working with Data</em>. O’Reilly. URL</p><p>[6] Fahrmeir, L., Künstler, R., Pigeot, I., &amp; Tutz, G. (2016). <em>Statistik: Der Weg zur Datenanalyse</em> (8th ed.). Springer.</p><p>[7] Montgomery, D. C. (2012). <em>Design and analysis of experiments</em> (8th ed.). Wiley. <a href="https://faculty.ksu.edu.sa/sites/default/files/douglas_c._montgomery-design_and_analysis_of_experiments-wiley_2012_edition_8.pdf">URL</a></p><p>[8] Moore, D. S., McCabe, G. P., Craig, B. A., &amp; Duckworth, W. M. (2021). <em>Introduction to the practice of statistics</em> (10th ed.). W. H. Freeman.</p><p>[9] Montgomery, D. C., Peck, E. A., &amp; Vining, G. G. (2012). <em>Introduction to linear regression analysis</em> (5th ed.). Wiley. <a href="https://ocd.lcwu.edu.pk/cfiles/Statistics/Stat-503/IntroductiontoLinearRegressionAnalysisbyDouglasC.MontgomeryElizabethA.PeckG.GeoffreyViningz-lib.org.pdf">URL</a></p><p>[10] Hosmer, D. W., Lemeshow, S., &amp; Sturdivant, R. X. (2013). <em>Applied logistic regression</em> (3rd ed.). Wiley.</p><p>[11] Johnson, R. A., &amp; Wichern, D. W. (2007). <em>Applied multivariate statistical analysis</em> (6th ed.). Pearson. <a href="https://alimoradi.iut.ac.ir/sites/alimoradi.iut.ac.ir/files/file_basepage/richard_arnold_johnson_dean_w._wichern_applied_bookzz.org_.pdf">URL</a></p><p>[12] Box, G. E. P., Jenkins, G. M., Reinsel, G. C., &amp; Ljung, G. M. (2015). <em>Time series analysis: Forecasting and control</em> (5th ed.). Wiley. <a href="http://repo.darmajaya.ac.id/4781/1/Time%20Series%20Analysis_%20Forecasting%20and%20Control%20%28%20PDFDrive%20%29.pdf">URL</a></p><p>[13] Witten, I. H., &amp; Frank, E. (2005). <em>Data mining: Practical machine learning tools and techniques</em> (2nd ed.). Morgan Kaufmann.<a href="http://academia.dk/BiologiskAntropologi/Epidemiologi/DataMining/Witten_and_Frank_DataMining_Weka_2nd_Ed_2005.pdf">URL</a></p><p>[14] Everitt, B. S., Landau, S., Leese, M., &amp; Stahl, D. (2011). <em>Cluster analysis</em> (5th ed.). Wiley. <a href="https://cicerocq.wordpress.com/wp-content/uploads/2019/05/cluster-analysis_5ed_everitt.pdf">URL</a></p><p>[15] Aggarwal, C. C., &amp; Reddy, C. K. (2014). <em>Data Clustering: Algorithms and Applications</em>. CRC Press <a href="https://people.cs.vt.edu/~reddy/papers/DCBOOK.pdf">URL</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OQyHoHVLRFPetgCp7nxOwQ.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa910001a3a7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/ascvit-v1-automatic-statistical-calculation-visualization-and-interpretation-tool-aa910001a3a7">ASCVIT V1: Automatic Statistical Calculation, Visualization and Interpretation Tool</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimize Open WebUI: Three practical extensions for a better user experience]]></title>
            <link>https://medium.com/pythoneers/optimize-open-webui-three-practical-extensions-for-a-better-user-experience-cbe365af60b1?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbe365af60b1</guid>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[open-webui]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[llama-3-1]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Mon, 02 Sep 2024 12:33:43 GMT</pubDate>
            <atom:updated>2025-03-03T21:41:32.372Z</atom:updated>
            <content:encoded><![CDATA[<h4>Instructions for setting up status emitters, word filters and memory functions to customize the interface and functions.</h4><p>In this article, I would like to discuss various options for customizing the interface and functionality of Open WebUI. I will discuss three specific examples. For all those who are not yet familiar with the application, the open source project Open WebUI makes it possible to use various AI models via a locally operated user interface without the need for complex programming skills. The models are integrated by the LLM runner Ollama. Alternatively, models can also be implemented in the GGFU format of Hugging Face. In the following, I will briefly discuss the setup of Open WebUI. If the application already exists, the following step can unfortunately not be skipped. <strong>Why? Because something needs to be considered for an example that will be shown.</strong></p><h4>HOW TO SET UP OPEN WEBUI</h4><p>Setup is possible using either Docker or Kubernetes. This article explains the setup with Docker. First, go to the official<strong> </strong><a href="https://www.docker.com/"><strong>Docker</strong></a> website and download Docker Desktop [DD], which can be found under <em>“Products”</em>, depending on the system you are using.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C8J_uQ6qfgkcWpujuOiTBg.png" /><figcaption>Get Docker Desktop [Own Screenshot Ollama]</figcaption></figure><p>Once DD has been installed, the application is started. There is currently no “container” available. What is a container? A container is an executable unit that contains an application and all its dependencies in an isolated package. The advantage here is that the application can be used on different systems and specific environmental conditions do not play a role.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsSq51BPkIpt-5yqAFx5qQ.png" /><figcaption>Currently, no container is available [Own Screenshot Ollama]</figcaption></figure><p>Before we take care of Open WebUI, Ollama must be installed. If Ollama already exists, this section can be skipped. If not, we will go to the official <a href="https://ollama.com/"><strong>Ollama</strong></a><strong> </strong>website and download the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X25aE_3rdYEDaLYlG2D0dw.png" /><figcaption>Get Ollama [Own Screenshot Ollama]</figcaption></figure><p>After installation, you should check whether Ollama is available or not, you may need to restart your system. You can use the following command to quickly check whether everything is working correctly.</p><pre>ollama --version</pre><p>The combination of Ollama and Open WebUI makes sense. The infrastructure for model management comes from Ollama, while the interface for interacting with the models is provided by Open WebUI.</p><p>We now open the official GitHub repository of <a href="https://github.com/open-webui/open-webui"><strong>Open WebUI</strong></a>. In the <em>“How to Install”</em> section, we find various commands to install the application. Depending on which command you choose, it still needs to be adapted. If Ollama is installed, the following command should be used according to the repository:</p><pre>docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main</pre><p>If you want Open WebUI to run with Nvidia GPU support, you must use the following command:</p><pre>docker run -d -p 3000:8080 --gpus all --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:cuda</pre><p>Depending on which command you choose, it must be slightly adapted so that one of the three extensions works in the interface.</p><pre>docker run -d -p 3000:8080 --gpus all --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --mount type=bind,source=&quot;C:\Users\scwom\Desktop\OPEN WEBUI FUNCTIONS\docker_data&quot;,target=/app/data --name open-webui --restart always ghcr.io/open-webui/open-webui:cuda</pre><p>Specifically, the command is extended by the following area.</p><pre>„--mount type=bind,source=&quot;C:\Users\scwom\Desktop\OPEN WEBUI FUNCTIONS\docker_data&quot;,target=/app/data“.</pre><p>What does this extension do? A directory of the local computer (host system) <em>“docker_data”</em> is integrated into the container. Data that is stored in the container <em>“target=/app/data”</em> becomes directly available on the local computer. The path must be adapted individually depending on where the directory is ultimately located! Using the bind mount shown here, it is therefore possible to exchange data even though the container is running in isolation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9_FTW2KhSRinMagNmT9HJg.png" /><figcaption>Bind Mount [Own illustration]</figcaption></figure><p>A bind mount cannot be set up for a container that is already running! Why is that? <strong>Because Docker does not allow this.</strong> To enable the exchange, a new container must be created. For this reason, we have adapted the command above. After installation, the container <em>“open-webui”</em> should be displayed in Docker Desktop. We click on the specified port and log in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gboY7DV5tVCL7QBKTgka1w.png" /><figcaption>Open WebUi Container [Own Screenshot Docker desktop]</figcaption></figure><p>If no model is available locally, it must be downloaded via Ollama. On the Ollama page, go to Models and select a model. Here you can see the current Llama3.1 model from Meta. The model can be loaded using the command <em>“ollama run llama3.1”</em>. After a system restart, it should be available in Open WebUI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7-kyEzWxABbsUkhUvaZTng.png" /><figcaption>Model management Llama 3.1 [Own Screenshot Ollama]</figcaption></figure><h4>FUNCTIONS IN OPEN WEBUI</h4><p>There are various functions that allow you to customize the user interface of Open WebUI and thus also the interaction with the models. This allows you to create your own individual WebUI. I will briefly discuss the various functions.</p><h4>FILTERS FOR THE INPUT AND OUTPUT OF THE MODEL</h4><p>Filters can be used to manipulate the outputs of the existing language models. A distinction is made here between two functions. The inlet function processes the user’s input before it is sent to the LLM. The input can thus be cleaned up or formatted. The outlet function processes the output of the model. Content that should not be output can be removed. In an educational context, filters are a useful feature to prevent the input and output of unwanted content. A filter comprises configuration options that are either defined by the administrator (Valves) or by the end user (UserValves). This allows aspects of the filter to be controlled (e.g. priority of filters) [1].</p><pre>class Filter:<br>    # Define and Valves<br>    class Valves(BaseModel):<br>        priority: int = Field(<br>            default=0, description=&quot;Priority level for the filter operations.&quot;<br>        )<br>        test_valve: int = Field(<br>            default=4, description=&quot;A valve controlling a numberical value&quot;<br>        )<br>        pass<br><br>    # Define any UserValves<br>    class UserValves(BaseModel):<br>        test_user_valve: bool = Field(<br>            default=False, description=&quot;A user valve controlling a True/False (on/off) switch&quot;<br>        )<br>        pass<br><br>    def __init__(self):<br>        self.valves = self.Valves()<br>        pass<br><br>    def inlet(self, body: dict, __user__: Optional[dict] = None) -&gt; dict:<br>        print(f&quot;inlet:{__name__}&quot;)<br>        print(f&quot;inlet:body:{body}&quot;)<br>        print(f&quot;inlet:user:{__user__}&quot;)<br><br>        # Pre-processing logic here<br><br>        return body<br><br>    def outlet(self, body: dict, __user__: Optional[dict] = None) -&gt; dict:<br>        print(f&quot;outlet:{__name__}&quot;)<br>        print(f&quot;outlet:body:{body}&quot;)<br>        print(f&quot;outlet:user:{__user__}&quot;)<br><br>        # Post-processing logic here<br><br>        return body</pre><h4>MORE INTERACTIVITY THROUGH ACTIONS</h4><p>Actions can be created to integrate interactive buttons into the Open WebUI user interface. The <em>“action”</em> function defines which type of action is executed and which data is processed [1].</p><pre>async def action(<br>        self,<br>        body: dict,<br>        __user__=None,<br>        __event_emitter__=None,<br>        __event_call__=None,<br>    ) -&gt; Optional[dict]:<br>        print(f&quot;action:{__name__}&quot;)<br><br>        response = await __event_call__(<br>            {<br>                &quot;type&quot;: &quot;input&quot;,<br>                &quot;data&quot;: {<br>                    &quot;title&quot;: &quot;write a message&quot;,<br>                    &quot;message&quot;: &quot;here write a message to append&quot;,<br>                    &quot;placeholder&quot;: &quot;enter your message&quot;,<br>                },<br>            }<br>        )<br>        print(response)</pre><h4>FROM PIPE TO PIPE TO MANIFOLDS</h4><p>Pipes can be used in Open WebUI to create models with user-defined logic and processing. In the WebUI, a pipe is an independent model. The primary logic to be executed is then defined in the <em>“pipe”</em> function. Several models can also be managed by one pipe. Compared to a pipe, a manifold is a group of models. They manage several pipes within a single system [1].</p><pre>class Pipe:<br>    class Valves(BaseModel):<br>        RANDOM_CONFIG_OPTION: str = Field(default=&quot;&quot;)<br><br>    def __init__(self):<br>        self.type = &quot;pipe&quot;<br>        self.id = &quot;blah&quot;<br>        self.name = &quot;Testing&quot;<br>        self.valves = self.Valves(<br>            **{&quot;RANDOM_CONFIG_OPTION&quot;: os.getenv(&quot;RANDOM_CONFIG_OPTION&quot;, &quot;&quot;)}<br>        )<br>        pass<br><br>    def get_provider_models(self):<br>        return [<br>            {&quot;id&quot;: &quot;model_id_1&quot;, &quot;name&quot;: &quot;model_1&quot;},<br>            {&quot;id&quot;: &quot;model_id_2&quot;, &quot;name&quot;: &quot;model_2&quot;},<br>            {&quot;id&quot;: &quot;model_id_3&quot;, &quot;name&quot;: &quot;model_3&quot;},<br>        ]<br><br>    def pipe(self, body: dict) -&gt; Union[str, Generator, Iterator]:<br>      # Logic goes here<br>      return body</pre><h4><strong>SHARED FUNCTION COMPONENTS</strong></h4><p>These functions can be used to provide dynamic details such as API keys or configuration options. As with the filters, a distinction is made between options that can be customized by the administrator (Valves) and those that can be customized by the end user (UserValves) [1].</p><h4>WHAT IS YOUR STATUS?</h4><p>Another useful function allows status information to be integrated into the exchange with the model via event emitters. This allows the user to be informed of progress in real time. This is particularly useful for more complex queries and the subsequent calculations. The ability to insert additional messages into the transmitted input at any time is also important in order to provide further information or report errors. For example, longer calculations of the model can be aborted because data has been transmitted incorrectly.</p><p>The <a href="https://docs.openwebui.com/tutorial/functions/"><strong>official documentation</strong></a> [1] of Open WebUI shows how the various functions are implemented as examples. Next, I will discuss the three examples I have created.</p><h4>NEW OPEN WEBUI FUNCTIONS</h4><p>The three examples take into account different functions, which I have already discussed. The first example is about a status emitter, the second example is about a filter, and the last example is about an action. The functions are available for download at the end of this article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CpWZovEJKHpQRNtd0p_elw.png" /><figcaption>The three new features we will create [Own illustration]</figcaption></figure><h4><strong>STATUS EMITTER [OIF EXAMPLE 1]</strong></h4><p>The first example belongs to the <em>“Tools”</em> class. An asynchronous method is implemented that simulates sequential processing. The progress is then sent to an event emitter. The <em>“asyncio”</em> module is required for asynchronous programming in Python. In the <em>“run”</em> function, the progress of the processing is represented by the variable<em> “__event_emitter__”</em>. The process is started, then a loop of 5 iterations <em>“for i in range(1, 6):”</em> is run through and finally the completion of the process is output. This adds a progress indicator to the user interface.</p><pre>import asyncio<br><br>class Tools:<br>    def __init__(self):<br>        pass<br><br>    async def run(self, prompt: str, __user__: dict, __event_emitter__=None) -&gt; str:<br>        &quot;&quot;&quot;<br>        The user is informed about the progress through an event emitter.<br>        &quot;&quot;&quot;<br>        if __event_emitter__:<br>            await __event_emitter__(<br>                {<br>                    &quot;type&quot;: &quot;status&quot;,<br>                    &quot;data&quot;: {&quot;description&quot;: &quot;Processing started...&quot;, &quot;done&quot;: False},<br>                }<br>            )<br><br>        for i in range(1, 6): <br>            await asyncio.sleep(1)  <br>            if __event_emitter__:<br>                await __event_emitter__(<br>                    {<br>                        &quot;type&quot;: &quot;status&quot;,<br>                        &quot;data&quot;: {<br>                            &quot;description&quot;: f&quot;Step {i} of 5 completed...&quot;,<br>                            &quot;done&quot;: False,<br>                        },<br>                    }<br>                )<br><br>        # View completion status<br>        if __event_emitter__:<br>            await __event_emitter__(<br>                {<br>                    &quot;type&quot;: &quot;status&quot;,<br>                    &quot;data&quot;: {<br>                        &quot;description&quot;: &quot;Processing completed!&quot;,<br>                        &quot;done&quot;: True,<br>                    },<br>                }<br>            )<br><br>        return &quot;Processing was completed successfully.&quot;</pre><p>Even if the display of status information is actually a function, the code must be integrated as a <em>“tool”</em>. In Open WebUI, we go to the <em>“Workspace” </em>area. Here we then click on <em>“Tools” </em>and on the plus symbol. In the following screenshot, the tools described above are already included.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*47Pdt86GHJMgr0uONGEstQ.png" /><figcaption>Create a new tool [Own Screenshot Open WebUI]</figcaption></figure><p>The code from the Python script is copied into the editor. A name for the tool is defined, and a description is entered. Finally, click on “Save”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qDQrn2qjKqYW_97C7S5R6w.png" /><figcaption>Paste the code into the editor and save it [Own Screenshot Open WebUI]</figcaption></figure><p>Alternatively, you can also integrate the tool via the community marketplace. In the <em>“Tool”</em> section, click on <em>“Discover a tool” </em>in the <em>“Made by OpenWebUI Community”</em> area and you will be redirected to an overview. Here you can search for @pietrusky under <em>“Tools”</em>. You can also simply use the following <a href="https://openwebui.com/t/pietrusky/status_emitter"><strong>link</strong></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SKW5yICCh14pAUrptZ5GDA.png" /><figcaption>Search the status emitter in the Community [Own Screenshot Open WebUI]</figcaption></figure><p>On the tool page, click on <em>“Get”</em> and then confirm <em>“Import to WebUI”</em>. The URL of the Docker container may need to be specified. Alternatively, you can load the tool as a .json file and then upload it accordingly in the <em>“Workspace”</em> area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XXmAFExfoLkLA4w-yzsTKw.png" /><figcaption>Get the status emitter from the OpenWebUI Community [Own Screenshot Open WebUI]</figcaption></figure><p>A new chat is started. Click on the plus symbol next to the entry and activate the tool for this conversation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aIMhOUVmL26AXBqJimBNhQ.png" /><figcaption>Activate the new tool [Own Screenshot Open WebUI]</figcaption></figure><p>In action, the status message then looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oDxbgoqowAq1zYo84JSAPw.gif" /><figcaption>This is what the status message looks like in action [Own GIF Open WebUI]</figcaption></figure><h4><strong>UNWANTED WORDS FILTER [OIF EXAMPLE 2]</strong></h4><p>Let’s move on to the second example. This is a function and not a tool. Specifically, it is a filter that prevents certain words from being processed and displayed in the chat history without the system message having to be adjusted. Let’s take a brief look at the function’s code. The <em>“Filter”</em> function contains two main methods <em>“inlet”</em> and <em>“outlet”</em>. The methods search the input and output for unwanted words. If a word is found, it is replaced by <em>“[filtered]”</em>. In this example, only <em>“sugar” </em>was defined as an unwanted word <em>“UNWANTED_WORDS”</em>. The list of words can be customized according to requirements and application area.</p><pre>from pydantic import BaseModel, Field<br>from typing import Optional, Dict<br>import re<br><br><br>class Filter:<br>    def __init__(self):<br>        pass  <br><br>    def inlet(self, body: dict, __user__: Optional[dict] = None) -&gt; dict:<br>        print(f&quot;inlet:{__name__}&quot;)<br>        print(f&quot;inlet:body before filtering:{body}&quot;)<br>        print(f&quot;inlet:user:{__user__}&quot;)<br><br>        UNWANTED_WORDS = [&quot;sugar&quot;]<br><br>        for message in body.get(&quot;messages&quot;, []):<br>            content = message.get(&quot;content&quot;, &quot;&quot;)<br>            print(f&quot;Original message content: {content}&quot;)<br>            for word in UNWANTED_WORDS:<br>                content = re.sub(<br>                    rf&quot;\b{word}\b&quot;, &quot;[filtered]&quot;, content, flags=re.IGNORECASE<br>                )<br>            message[&quot;content&quot;] = content<br>            print(f&quot;Filtered message content: {message[&#39;content&#39;]}&quot;)<br><br>        print(f&quot;inlet:body after filtering:{body}&quot;)<br>        return body<br><br>    def outlet(self, body: dict, __user__: Optional[dict] = None) -&gt; dict:<br>        print(f&quot;outlet:{__name__}&quot;)<br>        print(f&quot;outlet:body before filtering:{body}&quot;)<br><br>        UNWANTED_WORDS = [&quot;sugar&quot;]<br><br>        for message in body.get(&quot;messages&quot;, []):<br>            content = message.get(&quot;content&quot;, &quot;&quot;)<br>            for word in UNWANTED_WORDS:<br>                content = re.sub(<br>                    rf&quot;\b{word}\b&quot;, &quot;[filtered]&quot;, content, flags=re.IGNORECASE<br>                )<br>            message[&quot;content&quot;] = content<br><br>        print(f&quot;outlet:body after filtering:{body}&quot;)<br>        return body</pre><p>To be able to use the function in Open WebUI, it must also be created beforehand. We go back to the<em> “Workspace” </em>area and click on <em>“Functions”</em>. We click on the plus symbol again and copy the code from the file <em>“unwanted_words_filter.py” </em>into the editor. Alternatively, the function is also available in the community. Once the function has been created, it still needs to be activated. The functions can be activated globally for all existing models. To do this, click on the three dots and activate the <em>“global” </em>function. This saves the user from having to activate it for each individual conversation, as is unfortunately necessary with the tool described above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kbzpG0FS4266i55aUqVzzQ.png" /><figcaption>Activate the new function [Own Screenshot Open WebUI]</figcaption></figure><p>The display of <em>“[filtered]”</em> in the chat history may be slightly delayed. Irrespective of this, the defined words are no longer recorded by the LLM. The input and output then look as follows. The integration of this type of functionality is particularly useful in an educational context to prevent certain input and output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*noR97RCkCCg558hcD4vAXQ.png" /><figcaption>Conversation with new filter [Own Screenshot Open WebUI]</figcaption></figure><h4><strong>SAVE OUTPUTS [OIF EXAMPLE 3]</strong></h4><p>Let’s move on to the last example. A function is set up that executes an asynchronous action. Specifically, the last output of the LLM is saved in a file so that it can be used for further processing at a later date. I have already discussed the data exchange between the isolated container and the host system (bind mounts). The <em>“action”</em> function saves the content of the last output from “body” to a file. The function also contains an emitter <em>“__event_emitter__”</em> to inform the user that the output has been successfully saved. If an error occurs, this is also output.</p><pre>import os<br>from pydantic import BaseModel, Field<br>from typing import Optional<br><br><br>class Action:<br>    class Valves(BaseModel):<br>        pass<br><br>    class UserValves(BaseModel):<br>        show_status: bool = Field(<br>            default=True, description=&quot;Show status of the action.&quot;<br>        )<br>        pass<br><br>    def __init__(self):<br>        self.valves = self.Valves()<br>        pass<br><br>    async def action(<br>        self,<br>        body: dict,<br>        __user__=None,<br>        __event_emitter__=None,<br>        __event_call__=None,<br>    ) -&gt; Optional[dict]:<br>        print(f&quot;action:{__name__}&quot;)<br><br>        user_valves = __user__.get(&quot;valves&quot;)<br>        if not user_valves:<br>            user_valves = self.UserValves()<br><br>        if __event_emitter__:<br>            last_assistant_message = body[&quot;messages&quot;][-1]<br><br>            if user_valves.show_status:<br>                await __event_emitter__(<br>                    {<br>                        &quot;type&quot;: &quot;status&quot;,<br>                        &quot;data&quot;: {&quot;description&quot;: &quot;Saving to file&quot;, &quot;done&quot;: False},<br>                    }<br>                )<br><br>            try:<br>                directory = &quot;/app/data&quot;<br>                if not os.path.exists(directory):<br>                    os.makedirs(directory)<br><br>                file_path = os.path.join(directory, &quot;saved_outputs.txt&quot;)<br>                with open(file_path, &quot;a&quot;) as file:<br>                    file.write(f&quot;{last_assistant_message[&#39;content&#39;]}\n\n&quot;)<br>                print(&quot;Output saved to file in the container, accessible on the host.&quot;)<br><br>            except Exception as e:<br>                print(f&quot;Error saving output to file: {str(e)}&quot;)<br>                if user_valves.show_status:<br>                    await __event_emitter__(<br>                        {<br>                            &quot;type&quot;: &quot;status&quot;,<br>                            &quot;data&quot;: {<br>                                &quot;description&quot;: &quot;Error Saving to File&quot;,<br>                                &quot;done&quot;: True,<br>                            },<br>                        }<br>                    )<br><br>            if user_valves.show_status:<br>                await __event_emitter__(<br>                    {<br>                        &quot;type&quot;: &quot;status&quot;,<br>                        &quot;data&quot;: {&quot;description&quot;: &quot;Output Saved&quot;, &quot;done&quot;: True},<br>                    }<br>                )</pre><p>The output of the LLM is saved in a .txt file <em>“saved_outputs” </em>in the /app/data directory of the Docker container.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4gJQhyRRnpJZLhQln-gkmA.png" /><figcaption>File with saved outputs [Own Screenshot Docker desktop]</figcaption></figure><p>The customized command when setting up the Open WebUI container saves the .txt file on the local computer. The local directory <em>“C:\Users\scwom\Desktop\OPEN WEBUI FUNCTIONS\docker_data” </em>is bound to the directory <em>“/app/data”</em> within the container. The files that are saved in it are saved directly on the computer in the<em> “docker_data”</em> directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U6cwcBCoon865sGHGLV2IQ.png" /><figcaption>Saved file in docker_data folder [Own Screenshot]</figcaption></figure><p>The function is then created in the <em>“Workspace”</em> area, similar to the previous example. For the implementation, either use the code in the Python script or the <a href="https://openwebui.com/f/pietrusky/save_outputs"><strong>community page</strong></a>. As the last example is an action, an additional action area is created. For this example, I have selected a floppy disk as the icon for the button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YPxc8GFaZ9K0kyDGpMcPYA.png" /><figcaption>Conversation with new button [Own Screenshot Open WebUI]</figcaption></figure><p>The icon can be customized. All that is required is an appropriate format. Specifically, the icon is defined as an SVG graphic in Base64 format. The long character string specified for <em>“icon_url” </em>is interpreted by the user interface as a graphic. Icons in SVG format can be found on the following <a href="https://fonts.google.com/icons"><strong>Google page</strong></a>, for example. If you do not want cookies from Google, you must use a different source.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XS6Hh5s-mGosuM3Izt55-g.png" /><figcaption>Get the icon you want as an SVG graphic [Own Screenshot Google Fonts]</figcaption></figure><p>The graphic can then be converted to Base64 format using the following page. The .svg file is dragged into the corresponding field. After processing, click on <em>“show code”</em>, whereupon you will receive the character string you need <em>“data:image/svg+xml;base64,…”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eF32OwE93BAQGNg-iHMcqQ.png" /><figcaption>Convert your images to Base64 [Own Screenshot BASE64 IMAGE]</figcaption></figure><p>You can then adjust the background (black/white) in the overview. The character string can then be added to the code to change the icon of the button accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0DJPYIv56L15sAvm663img.png" /><figcaption>Copy the generated character string [Own Screenshot BASE64 IMAGE]</figcaption></figure><h4><strong>OIF PYTHON SKRIPTE [DOWNLOAD]</strong></h4><p>The files are available as Python script .py. If you want to upload the functions directly to Open WebUI, they must be converted to JSON .json format (better integrability). This is necessary even if error messages are displayed in the editor (e.g. Visual Studio Code). Now click on the folder to download the zip file with the three functions.</p><figure><a href="https://drive.google.com/file/d/1OqGfi7yjZLfhssPlWewxWQE7g-Xt0trk/view?usp=sharing"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aiFR8XNA1ywFllrS_lCWlQ.png" /></a><figcaption>Load the Python scripts by clicking on the folder [Own illustration]</figcaption></figure><h4>CONCLUSION</h4><p>In this article, three examples were used to show how the functionality of the Open WebUI interface can be extended using tools and functions. On the one hand, a status message for processing the input provided, a filter for certain words and a save function for the output were integrated. The examples shown are intended to inspire how the interface can be further optimized to improve the interaction between LLM and the user. Have fun customizing or creating your own functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*j6DnTo7-FNuMgfQOD6HrGA.gif" /><figcaption>You can clap up to 50 times!</figcaption></figure><p>[1] OpenWebUI. (n.d.). Functions. OpenWebUI Documentation. Abgerufen am [25.08.24], von <a href="https://docs.openwebui.com/tutorial/functions/">https://docs.openwebui.com/tutorial/functions/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail [Own illustration]</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbe365af60b1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pythoneers/optimize-open-webui-three-practical-extensions-for-a-better-user-experience-cbe365af60b1">Optimize Open WebUI: Three practical extensions for a better user experience</a> was originally published in <a href="https://medium.com/pythoneers">The Pythoneers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating Gradio-based AI chatbots into Moodle LMS by hosting on Hugging Face: A guide to…]]></title>
            <link>https://pub.towardsai.net/integrating-gradio-based-ai-chatbots-into-moodle-lms-by-hosting-on-hugging-face-a-guide-to-77775b42fcd5?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/77775b42fcd5</guid>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Sat, 24 Aug 2024 12:01:38 GMT</pubDate>
            <atom:updated>2025-03-03T21:43:03.622Z</atom:updated>
            <content:encoded><![CDATA[<h3>Integrating Gradio-based AI chatbots into Moodle LMS by hosting on Hugging Face: A guide to implementation and optimization</h3><h4>Learn how to use Gradio to integrate AI chatbots into Moodle, host models efficiently and improve your courses.</h4><h4>WHAT IS THE SIMPLEST METHOD?</h4><p>Recently, I have been thinking about how I can use the various possibilities in the field of machine learning in my seminars. At the same time, I want to be flexible when adapting and conducting experiments or studies. I came up with the idea of extending existing H5P activities with corresponding functionalities. I also thought about developing my own plugin for Moodle in order to be independent of H5P. After looking at documentation in this area, I thought to myself that there must be easier ways. My attempts to run models locally and establish a connection between the server hosting the LMS and my computer failed, but provided important insights for another way of integration. I then got involved with Gradio and the <em>“Spaces” </em>functionality of Hugging Face and was finally able to host an LLMs and implement it into Moodle’s LMS. In this post, I’ll go through the steps required to achieve this as well. Specifically, we will use Gradio to create a chat app that will then be hosted on Hugging Face to eventually embed it into Moodle.</p><p>The open-source library Gradio in Python enables developers to create simple and interactive user interfaces for machine learning models. In principle, it’s like Streamlit. Since Gradio was acquired by Hugging Face in 2021, the applications can be seamlessly connected to Hugging Face’s models and tools to be implemented as standalone apps. First, we create our Gradio chat app and test it locally. We create a Python script <em>“app.py”</em>.</p><h4>GRADIO CHAT-APP [IMPORT LIBRARIES]</h4><p>The required libraries are imported first. As already mentioned, we use <em>“gradio”</em> to create the user interface for our chat app. We use PyTorch <em>“torch”</em> for model processing. Depending on the model that will be used later, the code must be adapted accordingly (e.g. TensorFlow/Keras, JAX, MXNet). A pre-trained language model and tokenizer is loaded via the class “from transformers import AutoModelForCausalLM, AutoTokenizer” from the transformer&#39;s library. The “spaces” module is also imported to manage the GPU memory, more on this later.</p><pre>import gradio as gr<br>import torch<br>from transformers import AutoModelForCausalLM, AutoTokenizer<br>import spaces</pre><h4>GRADIO CHAT-APP [LOAD MODEL AND TOKENIZER]</h4><p>In addition to communicating with a model that is hosted via Hugging Face, the Gradio chat app can of course also be used locally. Depending on the model, it is important to check whether the necessary computing power is available. If you run the script locally, it checks whether a GPU (CUDA) is available, if not, the CPU is used. I have left this check in the script for the sake of completeness. The selection for applications that run via Hugging Face Spaces is done automatically in the background by their hosting service.</p><pre>device = &quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;</pre><p>Next, we load the <em>“model”</em> we want to use. For this article, I have selected a small model<em> </em><a href="https://huggingface.co/Qwen/Qwen2-0.5B-Instruct"><strong>Qwen/Qwen2–0.5B-Instruct</strong></a><strong> [1]</strong>, which can also be used with the free hardware <em>“Basic CPU (2 vCPU 16 GB RAM)”</em>. If you want to run the app locally and have enough RAM, you can of course also use a different model. The model is a causal language model <em>“CausalLM”</em>, i.e. it has been trained to predict text. The tokenizer is loaded to convert the user’s input into a form that the model can understand (tokenization). The output of the model is then converted back into text.</p><pre>model = AutoModelForCausalLM.from_pretrained(<br>    &quot;Qwen/Qwen2-0.5B-Instruct&quot;,<br>    torch_dtype=&quot;auto&quot;,<br>    device_map=&quot;auto&quot;<br>).to(device)<br>tokenizer = AutoTokenizer.from_pretrained(&quot;Qwen/Qwen2-0.5B-Instruct&quot;)</pre><h4>GRADIO CHAT-APP [GPU-DECORATOR]</h4><p>The decorator <em>“@spaces.GPU”</em> is intended to signal that the subsequent function <em>“def chatbot()” </em>can use GPU memory, which is required for the use of large models. With the free hardware of Hugging Face Spaces, current models (e.g. Llama3.1) cannot be used effectively. I will explain how it is still possible later in the article.</p><pre>@spaces.GPU</pre><h4>GRADIO CHAT-APP [CHATBOT FUNCTION]</h4><p>The chatbot is assigned a role by the system message <em>“system_message”</em>. In this example, it is a helpful assistant. This is followed by the definition of the <em>“messages”</em> list, which contains the conversation history and the current user input <em>“user_input”</em>. The system message is added at the beginning if there was no previous conversation <em>“if len(history) == 0”</em>. The system message can be customized depending on how the bot is to be used. The <em>“text” </em>is prepared using a special tokenizer template. The template is optimized for generation.</p><pre>def chatbot(user_input, history):<br>    system_message = {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: &quot;You are a helpful assistant.&quot;}<br>    messages = history + [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: user_input}]<br>    <br>    if len(history) == 0:<br>        messages.insert(0, system_message)<br>    <br>    text = tokenizer.apply_chat_template(<br>        messages,<br>        tokenize=False,<br>        add_generation_prompt=True<br>    )</pre><p>The prepared text is converted into tokens that the model understands <em>“model_input”</em>. The resulting tensor <em>“pt”</em> is moved to the existing device (CPU/GPU) <em>“to(device)”</em>. Which part of the input the model should pay attention to is signaled by a mask <em>“attention_mask”</em>. In this example, the entire input should be paid attention to. For this reason, the mask is set to ones <em>“torch.ones”</em>.</p><pre>    model_inputs = tokenizer([text], return_tensors=&quot;pt&quot;).to(device)<br>    attention_mask = torch.ones(model_inputs.input_ids.shape, device=device)</pre><p>The input forms the basis for the text that the model generates <em>“generated_ids”</em>. A maximum of 512 new tokens are generated. Depending on the available system memory for local operation or better HFS hardware, the value can be adjusted accordingly. The generated output is adjusted so that only the newly generated tokens are returned. Finally, the generated text is converted back into readable text <em>“response”</em>.</p><pre>    generated_ids = model.generate(<br>        model_inputs.input_ids,<br>        attention_mask=attention_mask,<br>        max_new_tokens=512<br>    )</pre><p>The conversation history is updated with the new input and the response <em>“history”</em>. As Gradio only understands a certain format, the history is transferred to this <em>“gradio_history”</em>. The updated history for the Gradio web app and the internal history is returned by the <em>“return”</em> function.</p><pre>generated_ids = [<br>        output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)<br>    ]<br>    <br>    response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]<br>    <br>    history.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: user_input})<br>    history.append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: response})<br>    <br>    gradio_history = [[msg[&quot;role&quot;], msg[&quot;content&quot;]] for msg in history]<br><br>    return gradio_history, history</pre><h4>GRADIO CHAT-APP [CREATE GRADIO INTERFACE]</h4><p>Now the user interface of the Gradio web app is created, which is structured in blocks like Streamlit <em>“gr.Blocks()”</em>. The conversation is saved in a typical representation of a chatbot <em>“chatbot_interface”</em> and the history is saved <em>“state = gr.State([])”</em>. A text box is created in which the user can make entries <em>“txt = gr.Textbox()”</em>. If the user makes an input <em>“txt.sumit()”</em>, the function <em>“chatbot()”</em> is called and the output is displayed in the chat window <em>“chatbot_interface”</em>. The Gradio webapp is finally started <em>“demo.launch()”</em>.</p><pre>with gr.Blocks() as demo:<br>    chatbot_interface = gr.Chatbot()<br>    state = gr.State([])<br><br>    with gr.Row():<br>        txt = gr.Textbox(show_label=False, placeholder=&quot;Gib hier eine Nachricht ein...&quot;)<br>        txt.submit(chatbot, [txt, state], [chatbot_interface, state])<br><br>demo.launch()</pre><h4>GRADIO CHAT-APP [TEST LOCALLY]</h4><p>To test the Gradio web app locally, any required libraries are installed via the terminal using <em>“pip”</em>. The import of the <em>“spaces”</em> module and the GPU decorator <em>“@spaces.GPU” </em>must be deleted or better commented out for the test.</p><pre>pip install torch transformers gradio</pre><p>Depending on whether the system used has a GPU, a specific version of <em>“torch”</em> must be installed. For CUDA 11.7, the command would be as follows:</p><pre>pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117</pre><p>The web app is finally started with the following command in the directory in which the <em>“app.py”</em> file is located.</p><pre>python app.py</pre><p>When the script is executed for the first time, the selected model, in this case <em>“Qwen/Qwen2–0.5B-Instruct”</em>, is downloaded from the Hugging Face Model Hub. As the model is very small, the download should not take very long. Gradio will start a local version of the webapp. The following output should be displayed in the terminal. You will also be informed that this is a development server and that it is not suitable for providing a product.</p><pre>Running on local URL:  http://127.0.0.1:7860/</pre><p>The link takes you to the actual web app.</p><figure><img alt="The image shows the gradio chat app." src="https://cdn-images-1.medium.com/max/1024/1*5BUePYgnVr6VMT6YmdM8sg.png" /><figcaption><em>The Gradio chat app Source: Image by author</em></figcaption></figure><p>The links to the <em>“spaces” </em>module can be integrated again. Next, we will take care of hosting the Gradio web app on Hugging Face Spaces to make it accessible via a URL.</p><h4>SETTING UP HUGGING FACE SPACES</h4><p>We create an account with Hugging Face. Once this is done, click on your profile and select the <em>“New Space”</em> option.</p><figure><img alt="The image shows the Hugging Face profile section." src="https://cdn-images-1.medium.com/max/1024/1*IR9UIHleyS1I6KUFbv31Tg.png" /><figcaption>Hugging Face profile section Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>Here we then define a name for the Space. The license is defined (e.g. Apache 2.0), we select <em>“Gradio”</em> as the Space SDK and the <em>“Blank”</em> template. We select <em>“CPU-based”</em> as the Space hardware. We initially set the visibility to <em>“Private”</em> and then create the Space. If you are not forwarded directly to the Space, you can select it via your profile area.</p><figure><img alt="The image shows how to create a new Space on Hugging Face." src="https://cdn-images-1.medium.com/max/1024/1*W3my7zurFt2jr6mRQiFBbw.png" /><figcaption>Create a new Space Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>In Space, we go to the <em>“Files”</em> area, which is structured in the same way as a repository on GitHub. The files <em>“.gitattributes”</em> and <em>“README.md” </em>are available by default. We click on<em> “+ Add file”</em> and select “Upload files”. We will upload two files in total. One is the already created Python script <em>“app.py” </em>and the other is the file <em>“requirements.txt”</em> to install the required libraries. The second file is a simple text editor <em>.txt</em> file and contains <em>“gradio”, “torch”, “transformers” </em>and <em>“accelerate”</em>. The latter is important to run machine learning models efficiently and automatically on different hardware configurations without complicated configurations. The files are added to the<em> “main branch” </em>and uploaded with <em>“Commit changes to main”</em>.</p><figure><img alt="The image shows the Hugging Face Space overview." src="https://cdn-images-1.medium.com/max/1024/1*UMf9UYAEO48fooPjZ1W1dg.png" /><figcaption>Hugging Face Space overview Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>The <em>“Building”</em> process should be running by now. As soon as the Gradio app is ready, it will be displayed next to the name of the space. Click on <em>“App”</em> and you will see the same display that you used locally before. You can now chat with the bot. Due to the configuration, it may take a while for the bot to respond.</p><figure><img alt="The image shows the running Gradio chat app on a Hugging Face Space." src="https://cdn-images-1.medium.com/max/1024/1*eM3Z2gAh00HMgeF7taBhew.png" /><figcaption>Gradio chat app HF Space Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>Since everything is working as planned, we go to the <em>“Settings”</em> of the space and change the visibility of the <em>“Make public”</em> space. This is necessary in order to be able to generate the codes for embedding.</p><figure><img alt="The image shows how to change Space visibility." src="https://cdn-images-1.medium.com/max/1024/1*oviz0DLN7d8GTc5W529Wtg.png" /><figcaption>Change Space visibility Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><figure><img alt="The image shows how to embed the created Space." src="https://cdn-images-1.medium.com/max/1024/1*ZU1aauxXVnWFoTgePiJTHQ.png" /><figcaption>Embed your Space Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>In the <em>“Copy Embed this Space” </em>window, the <em>“Web component” </em>code is copied and not the <em>“Iframe”</em> code. By embedding the web app via iframe, an additional network layer is added, which slows down communication overall. The content is also rendered with a delay via iframe and security functions in some browsers can restrict the execution of certain scripts. I have conducted a series of tests and can confirm that embedding via iframe causes the bot to take much longer to respond to input.</p><figure><img alt="The image shows the Web component code to embed the app." src="https://cdn-images-1.medium.com/max/1024/1*SDvpS-T5OiHcQ3GWW5nz8g.png" /><figcaption>Use web component code Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><h4>GRADIO WEBAPP IN MOODLE LMS</h4><p>Now the Gradio app is implemented in Moodle. We log in to an available system and go to a course in which we want to implement the app. In the course, we activate editing and create a <em>“Text and media field”</em> activity (in older versions, only <em>“Text field”</em>). In the activity, we activate the HTML view and paste the previously copied <em>“Web component” </em>code. The application that we previously saw and tried out locally and then via Hugging Face Space is now in LMS.</p><figure><img alt="The image shows the integration of  the web code into a Moodle activity." src="https://cdn-images-1.medium.com/max/1024/1*GSVLf4rKLf9vlOVuVeRIXw.png" /><figcaption>Integrate the web code into the LMS Source: <em>Image by author PHHD LMS</em></figcaption></figure><p>The implementation opens up many opportunities to further develop courses for students. I offer seminars in the field of machine learning and game development (2D/3D). By using models that specifically support programming, we can directly achieve added value. Participants can now communicate directly with the bot via the course. The app can provide support with tasks or generate food for thought, which promotes motivation and engagement in the learning process. Of course, models specifically tailored to the course can also be used or the functionality of the bot can be extended. For example, it could be possible to generate images that can then be used within projects. The availability of the bot can also be easily restricted via the visibility of the activity or made available to certain students (internal differentiation).</p><figure><img alt="The image shows the Gradio chat app in the Moodle LMS." src="https://cdn-images-1.medium.com/max/1024/1*JEbv6mvNddjuxYIDSLPCmg.png" /><figcaption>Gradio chat app in LMS Source: <em>Image by author PHHD LMS</em></figcaption></figure><h4>HOW TO USE CURRENT MODELS</h4><p>The procedure described for creating a Gradio webapp enables the use of smaller models without incurring costs. If you want to use larger and more up-to-date models with high quantization, you have to increase the hardware of the Spaces. There are various options for this in the <em>“Settings” </em>area. The most useful is the so-called PRO membership, which makes ZeroGPU (Nvidia A100) available. I don’t want to advertise here, but simply present a cost-effective configuration. In contrast to the basic CPU, Nvidia A100 is a state-of-the-art GPU that is designed for parallel calculations and is therefore better suited for machine learning and deep learning. The GPU is not permanently assigned to a space, but is used as required.</p><figure><img alt="The image shows how to change the Space hardware." src="https://cdn-images-1.medium.com/max/1024/1*NQIXreE5AkTDiTsj15kl6w.png" /><figcaption>Chance Space hardware Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>Alternatively, you can also go to the general Spaces overview and integrate the Spaces of other users into an LMS such as Moodle via web component code. The problem with this is that the Spaces may not be available when you need them. So let’s get back to setting up your own powerful environment.</p><figure><img alt="The image shows Spaces from other users on Hugging Faces." src="https://cdn-images-1.medium.com/max/1024/1*VmF61U2E5tg3Hu1_tHBOjw.png" /><figcaption>Use Spaces from other users Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><h4><strong>HARDWARE UPDATE</strong></h4><p>Once you have decided to upgrade, go to <em>“Settings”</em> and select the option. A payment method must be entered for hardware that is subject to a charge. Finally, select the hardware and click on <em>“Confirm new hardware”</em>. The web app setup starts from the beginning. As the Python script already contains the reference to <em>“spaces”</em> and the <em>“GPU decorator”</em>, no error message should appear. Alternatively, go to <em>“Dev mode”</em>, which can be used in the PRO membership. When the webapp is “Running” again, a more up-to-date model can now be implemented. For this article, I will use the current Llama3.1 model from Meta <strong>(</strong><a href="https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct"><strong>meta-llama/Meta-Llama-3.1–8B-Instruct</strong></a><strong> [2])</strong> to significantly improve the quality of the bot. To be able to use this model, access must be requested. Alternatively, you can use a model version quantized differently by the community.</p><figure><img alt="The image shows how to change the hardware." src="https://cdn-images-1.medium.com/max/1024/1*PFb4zgafGvncGRC27VKV1g.png" /><figcaption>Confirm hardware update Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><h4>CUSTOMIZATION OF THE PYTHON SCRIPT</h4><p>The Python script <em>“app.py”</em> must be adapted. An <em>“Access Token”</em> (AT) must be defined to authenticate the account so that the Space set up can access Llama3.1. This can be set up via the account settings. Go to <em>“Access Tokens” </em>and click on <em>“+ Create new token”</em>. Select <em>“Write”</em> as the type, give the AT a name and then click on <em>“Create token”</em>. The code of the AT is only displayed once, so you should save it accordingly. If you do not do this, you must create a new AT.</p><figure><img alt="The image shows that you have to agree the license agreements to use Llama3.1." src="https://cdn-images-1.medium.com/max/1024/1*WUmtsLV2YyEkQ1aM8waFgg.png" /><figcaption>Agree the license agreements Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><h4>YOUR SECRET API TOKEN</h4><p>Of course, it is not advisable to simply implement the generated AT in the <em>“app.py”</em> file, especially if the space is public. We will define an environment variable that contains the AT as a value and thus conceals it. This variable is then specified in the code and fulfills its purpose. Other users who see the code will not be able to do anything with it. In <em>“Settings”</em>, go to the <em>“Variables and secrets”</em> area. Click on <em>“New secret”</em>, specify a name (e.g.: HUGGINGFACEHUB_API_TOKEN) and the value (=AT) of the environment variable. Now let’s take care of customizing the script.</p><figure><img alt="The image shows the Access Tokens overview." src="https://cdn-images-1.medium.com/max/1024/1*2T_VzZTmPAtrlhouFqLw1g.png" /><figcaption>Create an Access Token Source: <em>Image by author </em><a href="https://huggingface.co/">Public Domain</a></figcaption></figure><p>The import of the <em>“os”</em> module is added. It provides functions to communicate with the operating system. We use it to read the previously defined environment variable. The API token is integrated to access the Llama3.1 model in the Hugging Face Hub. The value of the environment variable is retrieved <em>“os.getenv()”</em>. The new model that we want to use in our space is specified in <em>“model_name”</em>. In the tokenizer, the previously redefined API token is retrieved in order to access the protected model after authentication. This is also done when loading the <em>“model”</em> model. The model is loaded in a semi-precise floating point data type <em>“float16”</em>. This increases the computing power of modern GPUs and reduces memory consumption. To ensure that the input sensors <em>“model_inputs”</em> and the model <em>“model”</em> are executed on the same device (CPU/GPU), the model must be moved to the same device <em>“.to(device)”</em>. The script with the customized code is located in the file <em>“app-api_token.py”</em>. When it is uploaded to the created space, it should be renamed to <em>“app.py”</em>. When the setup process is complete, the chat webapp should now be able to do a lot more than before.</p><pre>access_token = os.getenv(&quot;HUGGINGFACEHUB_API_TOKEN&quot;)<br><br>model_name = &quot;meta-llama/Meta-Llama-3.1-8B-Instruct&quot;<br>tokenizer = AutoTokenizer.from_pretrained(model_name, token=access_token)<br>model = AutoModelForCausalLM.from_pretrained(model_name, token=access_token, torch_dtype=torch.float16).to(device)<br><br>model_inputs = tokenizer([text], return_tensors=&quot;pt&quot;).to(device)</pre><h4>PYTHON SCRIPT [DOWNLOAD]</h4><p>Click on the folder to download the zip file with the Python code.</p><figure><a href="https://drive.google.com/file/d/1OxJzrb0q4ik9UoMr4AstCKXWQcEnm3-W/view?usp=sharing"><img alt="The image shows a folder to download the Python files." src="https://cdn-images-1.medium.com/max/1024/1*fMKsGwlrCJFIEbVBvN2ROA.png" /></a><figcaption>Load the Python scripts by clicking on the folder Source: <em>Image by author</em></figcaption></figure><h4>CONCLUSION</h4><p>This article showed how to create a chat web app using Gradio. The app can be run locally and models can be exchanged. The performance of the existing system must be taken into account. We then set up a dedicated space on Hugging Face to host a model and the app so that others can access it. The chat webapp was then implemented in an LMS such as Moodle, which can provide added value in the context of courses. Finally, it was shown that more up-to-date models such as Llama3.1 can also be used. However, this requires membership of Hugging Face in order to be able to use better hardware. As an alternative to Gradio, other templates (e.g. Streamlit) can also be used with Hugging Face. Have fun customizing the application and testing different models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*j6DnTo7-FNuMgfQOD6HrGA.gif" /><figcaption>You can clap up to 50 times!</figcaption></figure><p>[1] Qwen Team. (2024). <em>Qwen2–0.5B-Instruct</em> [Repository]. Hugging Face. <a href="https://huggingface.co/Qwen/Qwen2-0.5B-Instruct">https://huggingface.co/Qwen/Qwen2-0.5B-Instruct</a></p><p>[2] Meta AI. (2024). <em>Meta-Llama-3.1–8B-Instruct</em> [Repository]. Hugging Face. <a href="https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct">https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail Source: <em>Image by author</em></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77775b42fcd5" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/integrating-gradio-based-ai-chatbots-into-moodle-lms-by-hosting-on-hugging-face-a-guide-to-77775b42fcd5">Integrating Gradio-based AI chatbots into Moodle LMS by hosting on Hugging Face: A guide to…</a> was originally published in <a href="https://pub.towardsai.net/">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The influence of illusion: How bots are changing the social media world]]></title>
            <link>https://levelup.gitconnected.com/the-influence-of-illusion-how-bots-are-changing-the-social-media-world-3509c33c6a79?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/3509c33c6a79</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[youtube]]></category>
            <category><![CDATA[ethical-hacking]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Wed, 14 Aug 2024 11:20:34 GMT</pubDate>
            <atom:updated>2025-03-03T21:44:48.965Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V0Nm8dSOAXVt2s0oD21Ntw.gif" /><figcaption>The bots are coming [Own gif]</figcaption></figure><h4>From theory to practice: Build your own YouTube View bot</h4><h4>NUMBERS, DATA AND FACTS</h4><p>According to Statista, the Google-owned video portal YouTube is in second place in the ranking of the largest social networks and messengers, with around 2.49 billion active users. Only Facebook (3.05 billion) has more active users. WhatsApp is in third place (2 billion) [1]. The total number of active social media users worldwide is now 5 billion and, according to the Statista Global Consumer Survey, has almost reached the number of internet users (5.4 billion) [2]. The figures make it clear that there are still many opportunities in the social media sector. As a result, there are more and more so-called influencers. Companies have long since incorporated this area into their portfolio of opportunities to promote their products. According to estimates by the Statista Research Department, the influencer marketing market is set to grow from the current 17.4 billion US dollars to 22.2 billion US dollars by 2025 [3]. In cooperation with “The Influencer Marketing Factory”, the Statista Research Department recorded the annual income of content creators (CC) in the USA in 2023. A quarter of CCs stated that they earn between 50,000 and 100,000 US dollars, with a similar number (24%) stating that they earn between 100,000 and 500,000 US dollars [4].</p><p>Based on these data and figures, it is probably not surprising that more and more young people can imagine becoming influencers themselves one day. Influencers themselves now say that what they do every day, in addition to promoting themselves, is a profession and not a hobby. The opportunities to be successful as an influencer, i.e. to build up a correspondingly large community in order to earn money through advertising partners, are now easier than you might think. Advances in the field of machine learning not only simplify the actual production (audio, image, text and video generation) of content, but can also help to optimize the reach and effectiveness of published content. In the long term, this technological progress means that sooner or later there will be more influencers than people who can actually or only apparently be influenced.</p><h4>WHAT THIS ARTICLE IS ABOUT!</h4><p>Based on the situation described, I would like to show in this article how to program a bot that generates YouTube views. Specifically, a video on YouTube is played in an automated browser with random proxies. The bot also simulates human behavior to reduce the likelihood of YouTube’s algorithms detecting the automated views. The development is for educational purposes only. The aim is to illustrate how easy it is to manipulate the parameters of well-known social media services and how practically anyone can become an influencer or fake influencer with any content (e.g. fake news, conspiracy myths, racist content, etc.). Of course, a certain technical affinity is required to set up the bot. YouTube was chosen at random for this post. In principle, the process can also be transferred to other providers (e.g. music streaming platforms). In addition to demonstrating pure manipulation, ethical questions should also be raised in the context of courses as to the extent to which certain figures in social media services are credible at all.</p><h4>THE COMPUTER DOESN’T SAY NO?</h4><p>If you were to ask an LLM (e.g. ChatGPT, Gemini, LLM model from HuggingFace via Open WebUI etc.) to generate a bot, you would first receive warnings. The development of such a bot would be illegal. The bot violates guidelines. Its use will have technical consequences (e.g. blocking of accounts) or there may even be a threat of legal prosecution. However, if you go online and enter <em>“Buy YouTube Views”</em> in a search engine, for example, you will receive countless offers from providers who use such bots to push or <em>“advertise”</em> content. Here you can buy packages with different volumes (100, 1,000, 10,000 or 100,000 views). Prices vary depending on the provider. These pages do not refer to bots, but to legitimate social media marketing. Depending on how these bots are programmed, the purchased views quickly disappear again because they are recognized by YouTube’s algorithms. Let’s get back to the output of the LLMs. If, after receiving the warning, you say that the development of such a bot is being carried out for research purposes, you will quickly receive information on what you need to do to implement it. Depending on the provider, you will receive more detailed answers and can then start development.</p><h4>BOT PREPARATION</h4><p>I will go through the points step by step that are required for the bot or Python script at the end of the article to work. Python should be installed on your system. Depending on your operating system, you can download the required execution file from the <a href="https://www.python.org/downloads/">official website</a>. After installation, you can check whether the installation was successful using the terminal with the following command:</p><pre>python --version</pre><p>After Python has been installed, the following packages must be installed: selenium (to control the web browser), beautifulsoup4 (to parse HTML content) and requests (to send HTTP requests and retrieve proxy lists). The installation is carried out via the terminal:</p><pre>pip install selenium beautifulsoup4 requests</pre><p>Next, we take care of the WebDriver, which plays an important role in the automation of the web browser. Specifically, it is a bridge between the Selenium script and the browser. The browser is controlled by the WebDriver based on the instructions specified in the script. Depending on the browser to be used later, a different WebDriver is required. In this example, I am using Firefox and therefore need the GeckoDriver to control the WebDriver API. The GeckoDriver can be downloaded from the following <a href="https://github.com/mozilla/geckodriver/releases">official site</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7VgCnVlsD8nWaX4BFOnlJw.png" /><figcaption>Interaction between the Selenium client, the WebDriver, and the browser [Own illustration]</figcaption></figure><p>The system must be configured accordingly so that the WebDriver can be found and used by Selenium. After the GeckoDriver has been downloaded, the directory is unpacked and the <em>“geckodriver.exe” </em>file is copied into a directory. I have named the directory YT_VB (YouTube View Bot) and created it on the desktop. I will explain the following steps in more detail for devices with Windows as the operating system. In the Control Panel, go to <em>“System and Security”</em>, then to <em>“System”</em>. Go to “<em>Advanced system settings” </em>via the information display and click on “Environment variables” in the <em>“System properties”</em>. In the overview of system variables, search for the variable “PATH” and implement the path to the directory where the <em>“geckodriver.exe”</em> file is located.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ixRa5hqfcUbIkkFqXIDgCQ.png" /><figcaption>Adjustment of the “Path” environment variable [Own illustration]</figcaption></figure><p>For macOS and Linux, the unpacked <em>“geckodriver.exe”</em> file can be moved to a directory that is contained in the path (e.g. /usr/local/bin). Alternatively, the path can be added permanently or temporarily. If the environment variable described has been expanded, everything must be confirmed and the system restarted for the adjustments to work. To check, you should enter the following command in the terminal after the restart.</p><pre>geckodriver --version</pre><p>If the version is displayed, everything has worked. Next, let’s move on to the actual Python script and how it works.</p><p>A total of three bots are available. The code for the basic version can be found in the file <em>“ytvb1.py”</em>. A customized bot, where a proxy IP list is created manually (.txt file) and the Python script accesses it, is located in the file <em>“ytvb2.py”</em>. The file also contains fewer comments.The bot with user interface is located in the file <em>“ytvb3.py”</em>. The different files make it easier to understand the different implementations and decide for yourself what you want to use. The basic version is explained step by step below. If a supplement is contained in another file, this will be mentioned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HakE_NnJnrMGicrcDa2xCQ.png" /><figcaption>The three bots (ytvb1, ytvb2 and ytvb3) [Own illustration]</figcaption></figure><h4>BOT PYTHON SCRIPT [IMPORT MODULE]</h4><p>The first step is to import the required modules. I have already mentioned what Selenium, Requests and BeautifulSoup do. For the implementation of waiting times in the script, <em>“time”</em> is used. To simulate human behavior and fool YouTube’s algorithms, <em>“random”</em> is used. Specifically, random times and movements are generated here as if you were scrolling down and up the page of the video. Both are part of the standard Python library and do not need to be installed separately.</p><pre>from selenium import webdriver<br>from selenium.webdriver.firefox.options import Options<br>from selenium.webdriver.common.by import By<br>from selenium.webdriver.support.ui import WebDriverWait<br>from selenium.webdriver.support import expected_conditions as EC<br>import time<br>import requests<br>from bs4 import BeautifulSoup<br>import random</pre><h4>BOT PYTHON SCRIPT [RETRIEVE PROXIES]</h4><p>The function <em>“get_proxies():”</em> retrieves a list of proxies from a website. The <a href="https://www.sslproxies.org/">following page</a> was used for this article. The function sends an HTTP request to the page <em>“requests.get(url)”</em>, parses the HTML response (BeautifulSoup) and extracts the proxy IP addresses and ports. For this to work, the table containing the proxy information must be found in the source code of the page. In this specific example, the table with the IPs on the page is referred to as the CSS class <em>“table table-striped table-bordered”</em>.</p><pre>def get_proxies():<br>    url = &#39;https://www.sslproxies.org/&#39;<br>    response = requests.get(url)<br>    if response.status_code != 200:<br>        raise Exception(f&quot;Failed to fetch proxies: Status code {response.status_code}&quot;)<br>    <br>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<br>    table = soup.find(&#39;table&#39;, {&#39;class&#39;: &#39;table table-striped table-bordered&#39;})<br>    if table is None:<br>        raise Exception(&quot;Could not find the proxy list table on the webpage.&quot;)<br>    <br>    proxies = []<br>    for row in table.find_all(&#39;tr&#39;)[1:]:<br>        cols = row.find_all(&#39;td&#39;)<br>        if len(cols) &lt; 2:<br>            continue<br>        proxy = f&quot;{cols[0].text}:{cols[1].text}&quot;<br>        proxies.append(proxy)<br>    return proxies</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HeUivEZCu20At_azlhxamg.png" /><figcaption>Get the Proxy-IPs from sslproxies.org [Screenshot]</figcaption></figure><p>An alternative to this procedure is to create a .txt file in which the “proxy IPs” are created in the form <em>“ip:port” </em>(e.g.: 192.168.1.1:8080). The code, specifically the <em>“get_proxies():”</em> function, must be adapted as follows. Care must be taken to keep the list up to date, as outdated or faulty proxies can lead to errors when the bot is executed. The implementation of this function is contained in the file <em>“ytvb2.py”</em>.</p><pre>def load_proxies_from_file(file_path):<br>    try:<br>        with open(file_path, &#39;r&#39;) as file:<br>            proxies = file.read().splitlines()<br>        print(f&quot;Successfully loaded {len(proxies)} proxies from file.&quot;)<br>        return proxies<br>    except FileNotFoundError:<br>        print(f&quot;Error: File not found - {file_path}&quot;)<br>        return []<br><br>proxies_file_path = &#39;proxyip.txt&#39;  # Path to the proxy list<br>proxies = load_proxies_from_file(proxies_file_path)</pre><p>In order to use only the functional proxies from the list, a validation of the proxy IPs can be added. The causes of proxy errors can be manifold. They are either outdated, no longer active, they may no longer be accessible due to firewall restrictions, the servers may be overloaded or the timeout value may be too low. The latter leads to no connection if the proxy is too slow. The function <em>“is_proxy_working()”</em> checks whether a proxy is working by sending a request to the <a href="http://httpbin.org/ip">following page</a>. If the request is successful (status code 200), the proxy is working. The function <em>“get_working_proxies()” </em>filters the list of proxies and only returns those that are working. The implementation of this function is contained in the file <em>“ytvb3.py”</em>.</p><pre>def is_proxy_working(proxy):<br>    test_url = &quot;http://httpbin.org/ip&quot;<br>    proxies = {<br>        &quot;http&quot;: f&quot;http://{proxy}&quot;,<br>        &quot;https&quot;: f&quot;http://{proxy}&quot;,<br>    }<br>    try:<br>        response = requests.get(test_url, proxies=proxies, timeout=15)<br>        if response.status_code == 200:<br>            return True<br>    except requests.RequestException as e:<br>        st.write(f&quot;Proxy check failed for {proxy}: {e}&quot;)<br>    return False<br><br>def get_working_proxies(proxies):<br>    working_proxies = [proxy for proxy in proxies if is_proxy_working(proxy)]<br>    st.write(f&quot;Found {len(working_proxies)} working proxies out of {len(proxies)}.&quot;)<br>    return working_proxies</pre><p>In general, different sources for proxies should be used to increase the likelihood that they will work. You should also update the list regularly. Since public proxy lists often contain non-functional proxies, you can also use paid services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uHareX6y6e4Ya6b4eJcAhQ.png" /><figcaption>Keep your proxies up to date [Own illustration]</figcaption></figure><p>The implementation of validation means that the bot’s process takes longer, as all entries in the list are first checked before the first request. If you do not want this, you can transfer the proxy functionality from the file <em>“ytvb2.py”</em> to <em>“ytvb3.py”</em>.</p><h4>BOT PYTHON SCRIPT [ACCEPT OR REJECT COOKIES]</h4><p>The function <em>“accept_cookies():”</em> is used to accept the cookie notice on the YouTube page. It waits until the button for rejection or acceptance is displayed “WebDriverWait”. In this example, the button to reject is activated. If everything is to be accepted, the code must be adapted accordingly. An XPath selector is used to find the corresponding button <em>“By.XPATH”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bXHJx0U3q34iRSKAFKsoCw.png" /><figcaption>Selenium (Automation in the web browser) [Screenshot]</figcaption></figure><p>The button is then activated. In addition, a few outputs are included to see which method was successful. Specifically, a fallback, i.e. a backup mechanism, has been built in to ensure that the cookie hint is activated. This ensures the functionality of the bot, as it can always happen that the first attempt to find the button fails due to timing problems or network delays. Alternatively, you can also increase the value of <em>“WebDriverWait”</em> to ensure that the page is fully loaded before an attempt is made to activate a specific element. The implementation of this function is important because each time you visit the YouTube page, its server recognizes a proxy IP address from the extracted list as a new user, causing the cookie display to appear again and again. Your own IP address is masked by the proxy IP address.</p><pre>def accept_cookies(driver):<br>    try:<br>        cookie_button = WebDriverWait(driver, 10).until(<br>            EC.element_to_be_clickable((By.XPATH, &#39;//span[text()=&quot;Reject all&quot;]/ancestor::button&#39;))<br>        )<br>        cookie_button.click()<br>        print(&quot;Accepted cookies.&quot;)<br>    except Exception as e:<br>        print(&quot;No cookie prompt found or could not click the button:&quot;, e)<br>        try:<br>            # Fallback<br>            cookie_button = WebDriverWait(driver, 10).until(<br>                EC.element_to_be_clickable((By.XPATH, &#39;//span[text()=&quot;Reject all&quot;]/ancestor::button&#39;))<br>            )<br>            cookie_button.click()<br>            print(&quot;Accepted cookies with alternative method.&quot;)<br>        except Exception as e:<br>            print(&quot;Alternative method failed. No cookie prompt found or could not click the button:&quot;, e)</pre><h4>BOT PYTHON SCRIPT [PLAY AND PAUSE VIDEO]</h4><p>The function <em>“play_and_pause_video():”</em> simulates the playback of a video on YouTube with random pauses and scrolling movements to simulate human behavior. Specifically, the playback time is selected between <em>“min_play_time”</em> and <em>“max_play_time” </em>and the video is paused randomly during this time. For a video call to count as a call, the video should be played for at least 30 seconds. Scrolling takes place in <em>“step_size”</em> steps. There are pauses between the scrolling movements <em>“scroll_pause_time”</em>, which are randomly within a range so that it does not appear too static. When a certain scroll height <em>“scroll_limit”</em> has been reached, the scroll direction is changed. Specifically, the bot first moves the window downwards and then upwards again. Another option would be to randomize the starting point in the video.</p><pre>def play_and_pause_video(driver, min_play_time, max_play_time):<br>    try:<br>        # Random playback duration between min and max<br>        play_time = random.uniform(min_play_time, max_play_time)<br>        print(f&quot;Playing video for {play_time:.2f} seconds.&quot;)<br>        <br>        # Play video<br>        play_button = WebDriverWait(driver, 10).until(<br>            EC.element_to_be_clickable((By.XPATH, &#39;//button[contains(@class, &quot;ytp-play-button&quot;)]&#39;))<br>        )<br>        play_button.click()<br><br>        # Split the playback duration and scroll during playback<br>        start_time = time.time()<br>        scroll_pause_time = random.uniform(5, 10)  <br>        step_size = random.choice([50, 75, 100]) <br>        scroll_direction = 1  <br><br>        current_position = 0<br>        scroll_limit = random.randint(300, 500)  <br><br>        while time.time() - start_time &lt; play_time:<br>            driver.execute_script(f&quot;window.scrollBy(0, {step_size * scroll_direction});&quot;)<br>            current_position += step_size * scroll_direction<br>            time.sleep(scroll_pause_time)<br><br>            if current_position &gt;= scroll_limit:<br>                scroll_direction = -1  <br>            elif current_position &lt;= 0:<br>                scroll_direction = 1  <br><br>            # Random pause during playback<br>            if random.random() &lt; 0.3:  <br>                pause_duration = random.uniform(3, 5)<br>                print(f&quot;Pausing video for {pause_duration:.2f} seconds.&quot;)<br>                play_button.click()  <br>                time.sleep(pause_duration)<br>                play_button.click()  <br><br>        # Pause video<br>        play_button.click()<br>        print(f&quot;Video paused after {play_time:.2f} seconds.&quot;)<br>    except Exception as e:<br>        print(&quot;Could not play and pause video:&quot;, e)</pre><h4>BOT PYTHON SCRIPT [MAIN SCRIPT]</h4><p>In the main part of the script, the URL of the video is specified <em>“video_url” </em>for which the views are to be increased by way of example. The number of repetitions is specified <em>“num_repeats”</em> as well as the minimum <em>“min_play_duration”</em> and maximum “max_play_duration” playback time of the video. The proxy list is retrieved <em>“proxies = get_proxies()” </em>and the WebDriver is configured to finally access YouTube with various proxies. The concrete process is that YouTube is opened, the cookies are accepted, the specific video is called up and played for a certain time. The browser is closed, and the process is repeated with another proxy. The reason why the main YouTube page is opened first, and then the actual video is played, is that it looks more human than simply starting a browser and opening the video straight away. Now, the link of the <em>“video_url”</em> argument needs to be adjusted. Specifically, the link to the video whose views are to be increased must be implemented.</p><pre># Video URL whose views are to be increased<br>video_url = &quot;https://www.youtube.com/watch?v=h2IsokP_knE&amp;list=PLhVrO77VPbRJnHg5RDu1JSaozdhTndzIp&quot;  # Hier die URL des gewünschten Videos einfügen<br><br># Number of repetitions to increase the views<br>num_repeats = 10  <br><br># Minimum and maximum duration of the video playback in seconds<br>min_play_duration = 60  <br>max_play_duration = 120  <br><br># Retrieve list of proxies<br>try:<br>    proxies = get_proxies()<br>    print(f&quot;Successfully fetched {len(proxies)} proxies.&quot;)<br>except Exception as e:<br>    print(f&quot;Error fetching proxies: {e}&quot;)<br>    proxies = []<br><br>def get_proxy_driver(proxy):<br>    options = Options()<br>    options.add_argument(&#39;--proxy-server=%s&#39; % proxy)<br>    driver = webdriver.Firefox(options=options)<br>    return driver<br><br>if proxies:<br>    # Repetitions with different proxies<br>    for i in range(num_repeats):<br>        proxy = proxies[i % len(proxies)] <br>        driver = get_proxy_driver(proxy)<br>        <br>        # Open YouTube homepage<br>        driver.get(&quot;https://www.youtube.com&quot;)<br>        accept_cookies(driver)<br>        driver.get(video_url)<br>        <br>        # Play and pause video<br>        play_and_pause_video(driver, min_play_duration, max_play_duration)<br>        <br>        # Close the browser to change proxy<br>        driver.quit()<br>else:<br>    print(&quot;No proxies available to use.&quot;)<br><br>print(&quot;Done&quot;)</pre><p>Depending on which version of the bot you choose, go to the directory containing the WebDriver execution file and the Python script. The bot is started via the terminal with the command:</p><pre>python ytvb1.py # Bot basic<br>python ytvb2.py # Bot with manual proxy IP list</pre><p>When the bot is running, the output in the terminal should look like this after the first run. Error messages are displayed that the WebDriver has problems finding the button in the cookie window. The fallback solves the problem. This is followed by output on how long a video is played and paused. The output is repeated depending on how many repetitions were specified in the “num_repeats” argument.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uDk_tCZuoiYzcsnoJ_o8ig.png" /><figcaption>YouTube View Bot Terminal Output [Screenshot]</figcaption></figure><h4>GUI FOR THE BOT!</h4><p>To make it easier to control the bot and adjust the various parameters more quickly, Streamlit can be used to create a graphical user interface. To be able to use Streamlit, it must first be installed using the following command:</p><pre>pip install streamlit</pre><p>To create the GUI elements, <em>“st.title()”, “st.text_input()”, “st.number_input()”, “st.slider()” </em>and <em>“st.text_area()”</em> are used. The bot is started via a button created with “st.button()”. The proxy IPs can be added via the text field, alternatively a drag-and-drop field has been set up into which the previously created .txt file can be dragged. If no file has been uploaded, the proxy IPs contained in the text field are used. If the automation process is not to run in the background but in the foreground, the following argument in the <em>“get_driver_with_proxy()”</em> function must be removed:</p><pre>options.add_argument(&#39;--headless&#39;)  </pre><p>Headless mode consumes fewer resources overall and also reduces the risk of unintentional user interaction, which could potentially interrupt the process. The bot with user interface then looks as follows and is started with the command:</p><pre>streamlit run ytvb3.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N9YOQoJSiHY0XETB_f7g9w.png" /><figcaption>YouTube View Bot [Screenshot]</figcaption></figure><h4>BOT PYTHON SCRIPT [DOWNLOAD]</h4><p>Click on the folder to download the zip file with the three bots.</p><figure><a href="https://drive.google.com/file/d/1GXvJbcszd1EQRDRR-9mny-hNzWFy48mI/view?usp=sharing"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6hJRDjQNHA9J4KEG7ZmLaA.png" /></a><figcaption>Load the Python scripts by clicking on the folder [Own illustration]</figcaption></figure><h4>ADDITIONAL NOTES</h4><p>Depending on when a video was uploaded, it may take longer or shorter for the views to be added to the video. If a video has just been uploaded to YouTube, it reacts more quickly to new views. For older videos, views are checked more strictly to avoid fraud. This is called the <em>“auditing”</em> phase. New videos only go through this phase when some time has passed since they were uploaded or when a certain number of views has been reached. This is also the reason why most videos receive a large number of views in the first few days and only very few after that. As part of a test series, the bot was tested on various videos that had had few views for a long time (mostly contributions from scientific conferences). After a certain period of time, the videos finally had more views.</p><h4>CONCLUSION</h4><p>This article showed how you can create a bot with simple means to increase the number of views of videos on YouTube. Only a basic setup was shown, which of course does not come close to what the numerous providers on the Internet offer in the area of social media marketing. Despite warnings at the beginning of the request, actual LLMs have assisted in the development of the bot. Sometimes suggestions came up to use Python in combination with Selenium for ethically safe data extraction or web scraping. If you tell the LLM that you no longer want any warnings about ethical concerns or that the bot is being developed in an educational context for demonstration purposes, these are no longer displayed. Thanks for reading and have fun customizing the bot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*j6DnTo7-FNuMgfQOD6HrGA.gif" /><figcaption>You can clap up to 50 times!</figcaption></figure><h4>SOURCES</h4><p>[1] We Are Social, &amp; DataReportal, &amp; Meltwater. (31. Januar, 2024). Ranking der größten Social Networks und Messenger nach der Anzahl der Nutzer im Januar 2024 (in Millionen) [Graph]. In Statista. Zugriff am 02. August 2024, von <a href="https://urlz.fr/rwzQ">https://urlz.fr/rwzQ</a></p><p>[2] Statista. (2024). Social Media Trends — Welche Plattformen sind am beliebtesten? Zugriff am 2. August 2024, von <a href="https://urlz.fr/rwzR">https://urlz.fr/rwzR</a></p><p>[3] HypeAuditor. (2. Februar, 2024). Entwicklung der Marktgröße des Influencer-Marketings weltweit in den Jahren 2021 bis 2025 (in Milliarden US-Dollar) [Graph]. In Statista. Zugriff am 02. August 2024, von <a href="https://urlz.fr/rwzT">https://urlz.fr/rwzT</a></p><p>[4] The Influencer Marketing Factory. (2. Mai, 2023). Was ist dein jährliches Einkommen als Content-Creator? [Graph]. In Statista. Zugriff am 02. August 2024, von <a href="https://urlz.fr/rwzX">https://urlz.fr/rwzX</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail [Own Illustration]</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3509c33c6a79" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/the-influence-of-illusion-how-bots-are-changing-the-social-media-world-3509c33c6a79">The influence of illusion: How bots are changing the social media world</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Talk to a PDF File Without Using Proprietary Models: CLI + Streamlit + Ollama]]></title>
            <link>https://medium.com/data-science/how-to-talk-to-a-pdf-file-without-using-proprietary-models-cli-streamlit-ollama-6c22437ed932?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c22437ed932</guid>
            <category><![CDATA[cli]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[ollama]]></category>
            <category><![CDATA[hands-on-tutorials]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Wed, 14 Aug 2024 08:05:41 GMT</pubDate>
            <atom:updated>2025-03-03T21:46:33.167Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dMbSpkta64YCd8PvJIJW8Q.gif" /><figcaption>Talk to a PDF file <em>(Gif by author)</em></figcaption></figure><h4>A contribution to the creation of a locally executed, free PDF chat app with Streamlit and Meta AI’s LLaMA model, without API limitations</h4><p>I have already read various articles on the internet about how the open source framework Streamlit can be used in combination with machine learning to quickly and easily create interesting interactive web applications. This is very useful for developing experimental applications without extensive front-end development. One article showed how to create a conversation chain using an OpenAI language model and then execute it. An instance of the chat model <em>“gpt-3.5-turbo”</em> was created, the parameter <em>“temperature”</em> was defined with a value of 0 so that the model responds deterministically and finally a placeholder for the API key was implemented. The latter is required to authenticate the model when it is used.</p><pre>llm = ChatOpenAI(model_name=&quot;gpt-3.5-turbo&quot;, temperature=0, api_key=&quot;&quot;)</pre><p>In the comments, I often read the question of how to deal with a particular error message or how it can be solved.</p><p><em>RateLimitError: Error code: 429 — {‘error’: {‘message’: ‘You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: </em><a href="https://platform.openai.com/docs/guides/error-codes/api-errors.&#39;,"><em>https://platform.openai.com/docs/guides/error-codes/api-errors.&#39;,</em></a><em> ‘type’: ‘insufficient_quota’, ‘param’: None, ‘code’: ‘insufficient_quota’}}</em></p><p>Error 429 indicates that the request sent to the OpenAI API has exceeded the current usage quota. The available API calls in a certain period or the general usage limit of the subscription have been reached. The error is easy to solve. You take out a paid subscription with the respective provider and thus increase your usage limit. This gave me the idea of why you can’t simply use open source models that are operated locally and thus circumvent the limitation without having to pay anything.</p><p>In this article, I will show how to use Streamlit to create an application to which PDF files can be uploaded in order to ask questions based on their content, which are answered by integrating an LLM. There are no limits or costs incurred when using the app. The response time (input-output) will take a little longer, depending on the system, but remains within reasonable limits. First, we take care of the LLM that we will use.</p><h4>A KINGDOM FOR A LLAMA</h4><p>We will use the open source language model Llama from Meta AI. As part of the recent development in the field of large language models, it will be used within the app to understand and generate natural language (NLP). In order to use the LLM locally, we first need to install Ollama on our system. To do this, we go to the following <a href="https://ollama.com/"><strong>official site</strong></a> and download the open source platform. The system may need to be restarted afterward.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zuJQuwxZWAsbaBHwj0fqxw.png" /><figcaption>Download Ollama (<a href="https://ollama.com/">Public Domain</a>)</figcaption></figure><p>Once Ollama has been installed, we click on<em> “Models” </em>and select the <em>“llama3.1”</em> model in the overview that opens. Llama is based on the Transformer architecture, has been trained on large and diverse data sets, is available in different sizes and is ideally suited for the development of practical applications due to its openness and accessibility. In this article, the smallest size <em>“8B”</em> is used to ensure that the app also works on less powerful systems. Once the correct model has been selected, copy the command shown and execute it in the terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zUMG2kSHz4Io9UK0habNcA.png" /><figcaption>LLama3.1 overview on Ollama platform (<a href="https://ollama.com/library/llama3.1">Public Domain</a>)</figcaption></figure><pre>ollama run llama3.1</pre><p>Once the model has been downloaded, you can communicate with it via the terminal. Next, let’s move on to setting up the app. In a nutshell, the process is as follows. The PDF file is uploaded and the text it contains is extracted. The extracted text is divided into smaller chunks that are stored in a vector store. The user enters a question. The question, i.e. the input, is prepared for the model by combining the question and the context. The LLM is queried and generates the answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ONaF6-tfN-odSsj1lD5PWQ.png" /><figcaption>App process (<em>Image by author)</em></figcaption></figure><h4>PDF CHAT APP [REQUIRED LIBRARIES]</h4><p>Various libraries are required for the application to function correctly, which are briefly described below. The execution of system commands in Python and communication with them is made possible by <em>“subprocess”</em>. We need <em>“streamlit” </em>to create the web application. <em>“PyPDF2”</em> is used to read PFD documents. The splitting of texts into smaller sections is done by <em>“langchain.text_splitter.RecursiveCharacterTextSplitter”</em>. The library <em>“langchain_community.embeddings.SpacyEmbeddings” </em>is used to generate text embeddings with the Spacy model. The vector store <em>“langchain_community.vectorstores.FAISS” </em>enables the efficient saving and retrieval of embeddings. For the definition of prompt templates for chat interactions, “<em>langchain_core.prompts.ChatPromptTemplate”</em> is used. Access to operating system functions is obtained via <em>“os” and “re”</em> is used to recognize patterns in character strings. Python should also be installed on the system. Depending on the operating system, the required execution file can be downloaded from the <a href="https://www.python.org/downloads/"><strong>official site</strong></a>. After installation, you can check whether the installation was successful using the terminal with the following command.</p><pre>python --version</pre><p>The required libraries are installed via the terminal with the following command:</p><pre>pip install streamlit PyPDF2 langchain langchain-community spacy faiss-cpu</pre><p><em>“subprocess”, “os” and “re” </em>are built-in Python libraries and do not need to be installed separately. The Spacy language model, however, must be downloaded separately with the following command.</p><pre>python -m spacy download en_core_web_sm</pre><p>The list of dependencies for the app is as follows.</p><pre>import subprocess<br>import streamlit as st<br>from PyPDF2 import PdfReader<br>from langchain.text_splitter import RecursiveCharacterTextSplitter<br>from langchain_community.embeddings.spacy_embeddings import SpacyEmbeddings<br>from langchain_community.vectorstores import FAISS<br>from langchain.tools.retriever import create_retriever_tool<br>from langchain_core.prompts import ChatPromptTemplate<br>import os<br>import re<br>import psutil</pre><p>Now that everything you need is in place, let’s move on to setting up the app. The various components of the script are described below.</p><h4>PDF CHAT APP [ENVIRONMENT CONFIGURATION]</h4><p>To avoid problems when loading libraries, especially during parallel processing, it is necessary to set the environment variable <em>“KMP_DUPLICATE_LIB_OK” </em>to <em>“TRUE”</em>. In the context of this article, this configuration is due to the use of FAISS [Facebook AI Similarity Search], which uses parallel computing operations when searching data sets.</p><pre>os.environ[&quot;KMP_DUPLICATE_LIB_OK&quot;] = &quot;TRUE&quot;</pre><h4>PDF CHAT APP [PDF READING FUNCTION]</h4><p>The <em>“pdf_read()”</em> function reads the entire text from a PDF file. Specifically, <em>“PyPDF2” </em>is used to extract the text. The text is then combined into a single character string <em>“text”</em>, which is returned. The function is important in order to make the content of the PDF file available for further processing steps.</p><pre>def pdf_read(pdf_doc):<br>    &quot;&quot;&quot;Read the text from PDF document.&quot;&quot;&quot;<br>    text = &quot;&quot;<br>    for pdf in pdf_doc:<br>        pdf_reader = PdfReader(pdf)<br>        for page in pdf_reader.pages:<br>            page_text = page.extract_text()<br>            if page_text:<br>                text += page_text<br>    return text</pre><p>In principle, several PDF files can be uploaded at the same time, whereby these together form the context. If you want to analyze individual files, you should only upload one file at a time, then remove it and upload a new file. The upload of multiple files, whereby these are viewed as individual contexts, is implemented in a customized version of the app.</p><h4>PDF CHAT APP [TEXT-CHUNKS FUNCTION]</h4><p>The combined character string from the previous function is split into smaller text chunks in the next step using the <em>“create_text_chunks()” </em>function. The maximum number of characters per chunk <em>“chunk_size”</em> is 1000 and the number of characters that can overlap in adjacent chunks <em>“chunk_overlap”</em> is 200. This implementation enables the app to query and process larger amounts of text more efficiently. Exceeding the input size of the model is prevented. The search is optimized by the split, as smaller, contextualized sections (granularity) can be queried more accurately (more detailed vectors), improving the provision of information overall. The accuracy and processing speed of the model are also increased.</p><pre>def create_text_chunks(text, chunk_size=1000, chunk_overlap=200):<br>    &quot;&quot;&quot;Create text chunks from a large text block.&quot;&quot;&quot;<br>    text_splitter = RecursiveCharacterTextSplitter(<br>        chunk_size=chunk_size,<br>        chunk_overlap=chunk_overlap<br>    )<br>    text_chunks = text_splitter.split_text(text)<br>    return text_chunks</pre><h4>PDF CHAT APP [TEXT-EMBEDDING]</h4><p>An object for text embeddings, numerical representation of text, using the Spacy model must be created to capture the meaning of the text uploaded as a PDF file. The embeddings are then used to vectorize the text in order to store them in a vector store so that they can be used for semantic search.</p><pre>embeddings = SpacyEmbeddings(model_name=&quot;en_core_web_sm&quot;)</pre><h4>PDF CHAT APP [VECTOR-STORE-FUNCTION]</h4><p>The <em>“vector_store()”</em> function uses the aforementioned FAISS to store the embeddings of the text chunks. The vector store enables faster retrieval and searching of texts based on the existing embeddings. The vector store is saved locally so that it can be accessed later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V6fKHGHQnBNuPeDr49JBPw.png" /><figcaption>From PDF to vector store <em>(Image by author)</em></figcaption></figure><pre>def vector_store(text_chunks):<br>    &quot;&quot;&quot;Create a vector store for the text chunks.&quot;&quot;&quot;<br>    vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)<br>    vector_store.save_local(&quot;faiss_db&quot;)</pre><p>Vectors capture the meaning and context of text by translating sentences and words into a mathematically interpretable space. The text “<em>The weather is nice today”</em> is converted into an example vector [0.25, -0.47, 0.19, …]. This makes it easier to carry out similarity searches.</p><h4>PDF CHAT APP [CLI BASED LLAMA REQUEST]</h4><p>The function <em>“query_llama_via_cli()” </em>enables communication with an external LLaMA model process via the command line. Input data is sent, the response is received, processed and any errors that occur are handled errors=’ignore’. This function allows the LLM to be implemented in the application workflow, although it runs in a separate environment that is controlled via CLI (Command Line Interface). The advantage of CLIs as a command line interface is that they are platform-independent, which makes the app available on almost any operating system.</p><pre>def query_llama_via_cli(input_text):<br>    &quot;&quot;&quot;Query the Llama model via the CLI.&quot;&quot;&quot;<br>    try:<br>        # Start the interactive process<br>        process = subprocess.Popen(<br>            [&quot;ollama&quot;, &quot;run&quot;, &quot;llama3.1&quot;],<br>            stdin=subprocess.PIPE,<br>            stdout=subprocess.PIPE,<br>            stderr=subprocess.PIPE,<br>            text=True,  # Ensure that communication takes place as text (UTF-8)<br>            encoding=&#39;utf-8&#39;,  # Set UTF-8 encoding explicitly<br>            errors=&#39;ignore&#39;,  # Ignore incorrect characters<br>            bufsize=1<br>        )<br>        <br>        # Send the input to the process<br>        stdout, stderr = process.communicate(input=f&quot;{input_text}\n&quot;, timeout=30)<br><br>        # Check error output<br>        if process.returncode != 0:<br>            return f&quot;Error in the model request: {stderr.strip()}&quot;<br><br>        # Filter response and remove control characters<br>        response = re.sub(r&#39;\x1b\[.*?m&#39;, &#39;&#39;, stdout)  # Remove ANSI codes<br><br>        # Extract the relevant answer<br>        return extract_relevant_answer(response)<br><br>    except subprocess.TimeoutExpired:<br>        process.kill()<br>        return &quot;Timeout for the model request&quot;<br>    except Exception as e:<br>        return f&quot;An unexpected error has occurred: {str(e)}&quot;</pre><p>A more detailed explanation of the function follows. The process is started by <em>“subprocess.Popen()”</em>. The command to start the LLM is executed [“ollama”, “run”, “llama3.1”]. Access to the input and output streams of the process (sending data and receiving results) is made possible by the parameters <em>“stdin”</em>, <em>“stdout”</em> and <em>“stderr”</em>. Communication takes place as UTF-8 encoded text <em>encoding=’utf-8&#39;</em>. To improve interactivity, the buffer size for the I/O operations is set to line buffering <em>“bufsize=1”</em>.</p><p>The input <em>“input_text”</em> is transmitted to the process, specifically to the LLM, whereupon a response is generated <em>“stdout”</em>. The maximum time (seconds) that is waited until the process has to deliver a response is 30 seconds “timeout=30”. If it takes longer, a timeout error is triggered <em>“stderr”</em>. The return code checks whether the process was successful <em>“returncode == 0”</em>. If this is not the case, an error message is returned. The app does not take as long to return a response. Finally, the response “stdout” is processed. Unwanted characters are removed and ANSI color and formatting codes are removed from the output <em>“response = re.sub(r’\x1b\[.*?m’, ‘’, stdout)”</em>. To extract and format the relevant response from the complete module response, <em>“extract_relevant_answer” i</em>s called. The process is terminated with <em>“process.kill()”</em> if the timeout of 30 seconds is exceeded. Errors that occur during communication are intercepted and returned as a general error message.</p><h4>PDF CHAT APP [EXTRACTION OF RELEVANT ANSWERS]</h4><p>The relevant response is extracted from the entire model response using the <em>“extract_relevant_answer()”</em> function. At the same time, the function also removes simple formatting problems, specifically spaces at the beginning and end of the composite character string that forms the response <em>“strip()”</em>. Depending on the specific requirements of the app, the function can be extended to return certain keywords or sentences (markers). The integration of additional rules for cleansing and formatting is also possible.</p><pre>def extract_relevant_answer(full_response):<br>    &quot;&quot;&quot;Extract the relevant response from the full model response.&quot;&quot;&quot;<br>    response_lines = full_response.splitlines()<br><br>    # Search for the relevant answer; if there is a marker, it can be used here<br>    if response_lines:<br>        # Assume that the answer comes as a complete return to be filtered<br>        return &quot;\n&quot;.join(response_lines).strip()<br><br>    return &quot;No answer received&quot;</pre><h4>PDF CHAT APP [THE CONVERSATION CHAIN]</h4><p>The conversation chain is created by the function <em>“get_conversational_chain()”</em>. The function prepares the input for the LLM by combining a specific prompt and the context together with the user’s question. The model should be provided with a clear and structured input in order to deliver the best possible answer. A multi-level prompt schema (system message, human message <em>“{input}”</em> and a placeholder) is defined by <em>“ChatPromptTemplate.from_message()”</em>. The role of the model is defined by the system message “system”. The human message then contains the user’s question. The prompt (behavior of the model), the context (content of the PDF file) and the question (user of the app) are combined in <em>“input_text”</em>. The prepared input is sent to the LLM via CLI using the function <em>“query_llama_via_cli(input_text)”</em>. The output is saved as <em>“response” </em>and displayed in the Streamlit app with <em>“st.write(“PDF: “, response)”</em>.</p><pre>def get_conversational_chain(context, ques):<br>    &quot;&quot;&quot;Create the input for the model based on the prompt and context.&quot;&quot;&quot;<br>    # Define the prompt behavior<br>    prompt = ChatPromptTemplate.from_messages(<br>        [<br>            (<br>                &quot;system&quot;,<br>                &quot;&quot;&quot;You are an intelligent and helpful assistant. Your goal is to provide the most accurate and detailed answers <br>                possible to any question you receive. Use all available context to enhance your answers, and explain complex <br>                concepts in a simple manner. If additional information might help, suggest further areas for exploration. If the <br>                answer is not available in the provided context, state this clearly and offer related insights when possible.&quot;&quot;&quot;,<br>            ),<br>            (&quot;human&quot;, &quot;{input}&quot;),<br>            (&quot;placeholder&quot;, &quot;{agent_scratchpad}&quot;),<br>        ]<br>    )<br><br>    # Combine the context and the question<br>    input_text = f&quot;Prompt: {prompt.format(input=ques)}\nContext: {context}\nQuestion: {ques}&quot;<br>    <br>    # Request to the model<br>    response = query_llama_via_cli(input_text)<br>    st.write(&quot;PDF: &quot;, response)  # The answer is displayed here</pre><h4>PDF CHAT APP [USER INPUT PROCESSING]</h4><p>The user input is processed and forwarded to the LLM using the <em>“user_input()”</em> function. Specifically, the entire text of the uploaded PDF file is used as the context. The conversation chain function <em>“get_conversational_chain”</em> is called to finally answer the user’s question <em>“user_question”</em> using the context <em>“context”</em> . In other words, the interaction between user and model is enabled by this function.</p><pre>def user_input(user_question, pdf_text):<br>    &quot;&quot;&quot;Processes the user input and calls up the model.&quot;&quot;&quot;<br>    # Use the entire text of the PDF as context<br>    context = pdf_text<br><br>    # Configure and request<br>    get_conversational_chain(context, user_question)</pre><h4>PDF CHAT APP [MAIN SCRIPT]</h4><p>The main logic of the Streamlit web application is defined by the <em>“main()”</em> function. Specifically, the Streamlit page is set up and the layout is defined. Currently, the layout only consists of the option to upload a PDF file <em>“st.file_uploader”</em> and an input field <em>“st.text_input”</em>. The user’s questions are entered in the latter. The user interface enables interaction with the model. If a PDF file is uploaded, it is read <em>“pdf_text = pdf_read(pdf_doc)”</em>. If there is still a question and the input has been confirmed, the request is processed <em>“user_input(user_question, pdf_text)”</em>.</p><pre>def main():<br>    &quot;&quot;&quot;Main function of the Streamlit application.&quot;&quot;&quot;<br>    st.set_page_config(page_title=&quot;CHAT WITH YOUR PDF&quot;)<br>    st.header(&quot;PDF CHAT APP&quot;)<br><br>    pdf_text = &quot;&quot;<br>    pdf_doc = st.file_uploader(&quot;Upload your PDF Files and confirm your question&quot;, accept_multiple_files=True)<br><br>    if pdf_doc:<br>        pdf_text = pdf_read(pdf_doc)  # Read the entire PDF text<br><br>    user_question = st.text_input(&quot;Ask a Question from the PDF Files&quot;)<br><br>    if user_question and pdf_text:<br>        user_input(user_question, pdf_text)<br><br>    # Monitor RAM consumption<br>    process = psutil.Process(os.getpid())<br>    memory_usage = process.memory_info().rss / (1024 ** 2)  # Conversion to megabytes<br>    st.sidebar.write(f&quot;Memory usage: {memory_usage:.2f} MB&quot;)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>The app described is started with the following command and looks like this.</p><pre>streamlit run pca1.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NtMMtNFQQ_c-Iff7qfzsvQ.png" /><figcaption>Streamlit PDF chat app 1 <em>Image by author</em></figcaption></figure><h4>INDIVIDUAL CUSTOMIZATION OPTIONS</h4><p>There are individual customization options in various areas. To improve the quality and relevance of the LLM’s responses, the system prompting or the system message, see Conversation chain, can be adapted. The behavior of the model can be directly controlled by specific instructions and context. You can experiment with different prompts to test how the model’s responses change. The current system message still offers a lot of potential for customization.</p><pre>    prompt = ChatPromptTemplate.from_messages(<br>        [<br>            (<br>                &quot;system&quot;,<br>                &quot;&quot;&quot;You are an intelligent and helpful assistant. Your goal is to provide the most accurate and detailed answers <br>                possible to any question you receive. Use all available context to enhance your answers, and explain complex <br>                concepts in a simple manner. If additional information might help, suggest further areas for exploration. If the <br>                answer is not available in the provided context, state this clearly and offer related insights when possible.&quot;&quot;&quot;,<br>            ),</pre><p>Another customization option is to replace the currently used LLM Llama3.1. Various models (e.g. gemma2, mistral, phi3, qwen2 etc.) in different sizes (2B, 8B, 70B etc.) are available on OLLAMA. To be able to use a different model, it must first be downloaded and then the function <em>“query_llama_via_cli()” </em>in the Python script must be adapted. Specifically, the command <em>[“ollama”, “run”, “llama3.1”]</em>, which starts the LLM, must be changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wikq0ZuMkc2rUecjYL5Ayw.png" /><figcaption>Some Ollama models <em>(Image by author)</em></figcaption></figure><p>When using other models, it must be ensured that the available computing power is sufficient for local use. You also need to consider whether the model will be executed on GPUs or CPUs. The following example can be used as a rule of thumb to assess whether a model will work on your own computer. A model with 1 billion parameters (1B) requires around 2 to 3 GB RAM (2–3 bytes per parameter). The Task Manager (Windows) can be used to check to what extent the execution of the application affects the performance of the system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q4bZjEElUS4AzsBIMEBNyw.png" /><figcaption>Windows Task Manager (<em>Image by author)</em></figcaption></figure><p>Alternatively, you can also integrate the application’s memory usage directly into Streamlit. This is done using the <em>“psutil”</em> library, which must first be installed and then implemented in the Python script.</p><pre>    # Monitor RAM consumption<br>    process = psutil.Process(os.getpid())<br>    memory_usage = process.memory_info().rss / (1024 ** 2)  # Conversion to megabytes<br>    st.sidebar.write(f&quot;Memory usage: {memory_usage:.2f} MB&quot;)</pre><p>It is also possible to customize the layout and functionality of the application. For example, the main logic, specifically the <em>“main()”</em> function, can be extended to allow the simultaneous upload of multiple PDF files <em>“accept_multiple_files=True”</em>. The files can then be displayed in a list and selected by the user <em>“selected_pdf_file”</em>. Processing then takes place as usual. Depending on the selected file, the extracted context is forwarded to the LLM together with the user’s question. The customized code can be found in the file <em>“pca2.py”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sq4hyH98nALoRk9YtPZEsA.png" /><figcaption>Streamlit PDF chat app 2 <em>(Image by author)</em></figcaption></figure><h4>PCA PYTHON SCRIPT [DOWNLOAD]</h4><p>Click on the folder to download the zip file with the two apps!</p><figure><a href="https://drive.google.com/file/d/15x3i1ov7GkOGWbml3LY_Os-DVR8kN98C/view?usp=sharing"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NIVXNC5JWVG1ZJmvAvUPUg.png" /></a><figcaption>Load the Python scripts by clicking on the folder (<em>Image by author)</em></figcaption></figure><h4>CONCLUSION</h4><p>This article showed how Python in combination with tools such as Streamlit, FAISS, Spacy, CLI, OLLAMA and the LLM Llama3.1 can be used to create a web application that allows users to extract text from PDF files locally, save it in the form of embeddings and ask questions about the content of the file using an AI model. By further optimizing the scripts (e.g. prompt), using other models and adapting the layout to include additional functionalities, the app can offer added value in everyday life without incurring additional costs. Have fun customizing and using the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*j6DnTo7-FNuMgfQOD6HrGA.gif" /><figcaption>You can clap up to 50 times!</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c22437ed932" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-talk-to-a-pdf-file-without-using-proprietary-models-cli-streamlit-ollama-6c22437ed932">How to Talk to a PDF File Without Using Proprietary Models: CLI + Streamlit + Ollama</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using the AI platform Captain for generation of images to simplify video game development]]></title>
            <link>https://medium.com/@stefanpietrusky/using-the-ai-platform-captain-for-generation-of-images-to-simplify-video-game-development-521f1e7d97e0?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/521f1e7d97e0</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[image-generation]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Thu, 20 Jun 2024 12:21:58 GMT</pubDate>
            <atom:updated>2025-03-03T21:48:08.005Z</atom:updated>
            <content:encoded><![CDATA[<p>Developing video games is a complex undertaking. A story has to be written. The story has to be told using visualizations. Music and sound effects should then add an atmospheric touch. This is followed by programming, which logically connects everything. These requirements pose major challenges for individual developers. Thanks to advances in the field of machine learning, various models are now available that can help indie developers with one or even all the challenges. In the last few weeks, I have developed several 2D video games that I need for a new concept that I am currently working on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UH2mDflwmKl3SuSHAx2BYA.gif" /><figcaption>Short impression of a current game project</figcaption></figure><h4>SHORTEN DEVELOPMENT TIME</h4><p>During the development process, I came up with the idea that I could also generate the visualizations (sprites) in the game using text-to-image models. The main advantage of this is that it significantly reduces development time, as I don’t have to draw everything myself or search for suitable asset bundles on the Internet, meaning I can concentrate on other aspects of development. I can also quickly and easily increase the replay value of the game by designing different levels. In this post, I would like to discuss the AI ​​tool <a href="https://github.com/blib-la/captain"><strong>Captain</strong></a> from the AI ​​startup blib-la, which I found on GitHub.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*ndyL3z4onGdrEXqpDWVMmA.png" /><figcaption>blib-la / captain GitHub Repository</figcaption></figure><h4>WHAT IS CAPTAIN?</h4><p>In the repository, Captain is described as an AI platform that turns my computer into an AI brain. The application is advertised as keeping 100% of my data on my computer. The application is also supposed to be easy to use, specifically, if I can use a computer, I can work with Captain. No programming knowledge is required. For less technically minded people, this is certainly an advantage. The AI ​​apps in the dashboard are supposed to be free to use. Captain is currently still in alpha release. The application works best with <strong>Windows </strong>with an <strong>NVIDIA GPU</strong> when the <a href="https://developer.nvidia.com/cuda-downloads?target_os=Windows&amp;target_arch=x86_64"><strong>NVIDIA CUDA Toolkit</strong></a> for Windows is installed and the latest <a href="https://www.nvidia.com/download/index.aspx"><strong>NVIDIA drivers</strong></a> are installed. The following steps are required to install the application.</p><h4>INSTALL CAPTAIN</h4><ol><li><strong>Download Captain:</strong> Go to the <a href="https://github.com/blib-la/captain/releases"><strong>releases page</strong></a> to get the Captain installer or click the download button above in the repository.</li><li><strong>Start Installation:</strong></li></ol><ul><li>Open the file you downloaded (Captain-Setup-x.x.x.exe).</li><li>Because Captain is in ALPHA and not signed by Microsoft, you might see a warning. This is normal for now and doesn’t mean it’s unsafe. Captain is open source, so you can check their code anytime.</li><li>Click on <em>“More Info”</em>, then click <em>“Run anyway”</em> to continue.</li></ul><p>3. <strong>Choose Install Location:</strong></p><ul><li>Pick where you want Captain installed on your computer.</li></ul><p>4. <strong>Install Captain:</strong></p><ul><li>Run the installer and follow the on-screen instructions. Some steps may take a bit longer because large files need to be downloaded.</li></ul><p>5. <strong>Complete Setup:</strong></p><ul><li>After all files are downloaded, Captain will start up.</li><li>Captain needs Qdrant to operate properly. If asked to allow <a href="https://qdrant.tech/"><strong>Qdrant</strong></a> network access, please allow it.</li></ul><h4>CAPTAIN DASHBOARD</h4><p>After the installation is complete, you start the application and go to the dashboard. There are currently 6 apps available (Assistant, ComfyUI, Explorer, Generate Images, Live Painting and Stories). Using the keyboard shortcut CTRL + ALT + Space, you can open a special window (prompt) to interact with the application, or you can simply click on the app you want to use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*Qb9DB06ruS1WYurfCQ9upA.png" /><figcaption>Captain Dashboard</figcaption></figure><p>Chats with different AI models can be started via the <em>“Assistant”</em> app. The selection is predefined; no other models can currently be added. In order to use the existing models, you have to store the corresponding keys in the application settings, which can be generated free of charge by the respective providers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*WjkC8Rk-ZMQNqKWBnSwogA.png" /><figcaption>Captain Assistant App</figcaption></figure><p>The second application is ComfyUI, which is an easy-to-understand user interface for working with Stable Diffusion and other AI models for image generation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bvtEZtBFiJFhHFxOuIsxBQ.png" /><figcaption>Captain ComfyUI App</figcaption></figure><p>The other two apps allow you to draw in real time in combination with text input and to generate stories to match the images you have previously created.</p><h4>IMAGE GENERATION</h4><p>The app that I want to test as an example for generating sprites is traditionally called <em>“Generate Images”</em>. When the app is started for the first time, a model (Stable Diffusion XL) must be downloaded. Depending on the internet speed, the download may take a while. After I have written what I want in the prompt and what I don’t want in the negative prompt, defined the art style and set the format, I generate the images that I need for my video game. For the game, I wanted a visualization for a floor plate that can be used again and again in the middle of the arena. The corners, sides and entrances or the rest of the level are created as a percentage by an algorithm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*Z1liEkp7KeCwcAuOpRL6ew.png" /><figcaption>Captain Text to Image App</figcaption></figure><p>To export the images created with Captain, go to Explorer and enter <em>“image”</em> in the search. All images that have been created will then be displayed. You can then drag the desired visualization from Explorer to the desktop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/1*Ko_jEKhVpBVhAVJAFPKmLw.png" /><figcaption>Captain Explorer</figcaption></figure><p>Alternatively, you can access the saved files via the following path:</p><pre>C:\Users\&lt;username&gt;\AppData\Roaming\captain__development__\Captain_Data\files\images</pre><h4>IMPLEMENTATION INTO THE GAME</h4><p>In my current game, the sprite for the central floor tiles <em>“sFloor1” </em>consists of four frames that are randomly combined to alternately fill the playing area completely. The game is played from a bird’s eye view. The format of the sprites is 512 x 512 px. The image created with the help of Captain therefore still needs to be scaled appropriately. To do this, I use the free image editing program GIMP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mTAQJQFPWhj_MpIBu04REw.png" /><figcaption>GMS2 IDE sFloor1 Sprite (4 Frames)</figcaption></figure><p>The adjusted image is defined in GM as a new sprite <em>“sFloor1” </em>and the game is started.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DGvYs1S_FIYj03ThtoWSvA.gif" /><figcaption>Short impression of a current game project (new floor)</figcaption></figure><p>The design of the level was adapted in a very short time. The next step was to create images for the corners, sides and entrances to the arena. The implementation shown here is for demonstration purposes only. This article showed how text-to-image models can be used to create visualizations in order to shorten the development time of video games. The open source application Captain, the alpha version of which is provided via the repository, already has functions that can support users in development without having to deal with complicated structures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=521f1e7d97e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Development of a chatbot in Botpress with own knowledge base and implementation in Moodle]]></title>
            <link>https://medium.com/@stefanpietrusky/development-of-a-chatbot-in-botpress-with-own-knowledge-base-and-implementation-in-moodle-to-55d3a46cbaff?source=rss-700be654e078------2</link>
            <guid isPermaLink="false">https://medium.com/p/55d3a46cbaff</guid>
            <category><![CDATA[moodle]]></category>
            <category><![CDATA[botpress]]></category>
            <category><![CDATA[wordpress]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Stefan Pietrusky]]></dc:creator>
            <pubDate>Fri, 12 Apr 2024 09:45:53 GMT</pubDate>
            <atom:updated>2025-03-03T21:49:20.258Z</atom:updated>
            <content:encoded><![CDATA[<h3>Development of a chatbot in Botpress with own knowledge base and implementation in Moodle to support students</h3><p>AI agents or chatbots to automate and optimize business tasks or support customer queries are widely used in business. The development and implementation of such bots to support students in lessons or students in seminars is constantly increasing. Likewise, LLMs such as ChatGPT are already being used by learners outside educational institutions to complete tasks. There are now various options for setting up your own bot (e.g. Rasa, Amazon Lex etc.) and hosting it (AWS [via EC2], Google Cloud, Heroku, DigitalOcean etc.). While setting up the bot is less of a hassle, the hosting can quickly become overwhelming. To simplify the procedure, this article presents the development of a chatbot using the Botpress framework. The bot is then implemented into an LMS such as Moodle to help teachers answer repetitive organizational questions from learners.</p><h3><strong>INTRODUCTION TO BOTPRESS</strong></h3><p>First, we go to the <a href="https://botpress.com/">Botpress website</a> and create a free account.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VhDWqae-TG8WPlpD5l5sCw.png" /><figcaption>Visit the Botpress page and register for free.</figcaption></figure><p>The free plan is more than sufficient to demonstrate the procedure. Here you can create a total of 5 bots, has 100 MB vector database storage, $5 AI credits, 5000 table rows, 2,000 messages can be received per month and up to 3 people can collaborate on the created bots via a shared workspace. A credit card is not required to create an account. After you are logged in to Botpress, you will be redirected to the Botpress Cloud and end up in your individual workspace.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kuf6XJafdOheWDe4Wglprw.png" /><figcaption>Your individual workspace with the bots you have created.</figcaption></figure><p>There are currently two bots in the workspace. If there is no bot, click on the blue “+ New Bot” button. You select the bot and then click on “Edit in Studio”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nmcmrHqSdpg3pkXEAhg4iw.png" /><figcaption>Go to Botpress Studio via the bot’s dashboard.</figcaption></figure><p>If you want to start your first bot or edit it in the studio, you will probably be forced to use the <em>“Learn Botpress”</em> template. If that is the case, simply create a bot and select <em>“Start from Scratch”</em> as the template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q0grXROvcsl9IJVVjyWYlQ.png" /><figcaption>We select an empty template “Start from Scratch”.</figcaption></figure><p>Finally, when you are in Botpress Studio, you either select the Knowledge Base (KB), which is present by default, or you create a new one using the Book Icon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4cngmJtdoHP6xWLTLJu0yQ.png" /><figcaption>In Botpress Studio, we select the “Default Knowledge Base” (KB) or create a new one.</figcaption></figure><p>Data can be made available to the KB in various formats. This can be done directly via a text editor, by uploading a document, a table or external data sources such as specific websites. A general internet search can also be a KB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L646jnwK8z0ib4ELjo5Ptw.png" /><figcaption>The KB could be provided with data in various formats.</figcaption></figure><p>The KB is the central component or basis for understanding the bot, as it contains the data required to fulfill user requests or tasks. The advantage here is that you can define exactly what the bot can answer and what it cannot. In this way, unwanted or misleading feedback is prevented for the user.</p><p>For this article, I created a document that answers 10 FAQs as examples that concern a seminar that I am conducting at the university in the current semester. This specifically covers the basics of 2D game development. After the document is uploaded, you can test the bot straight away and ask a question. To do this, click on <em>“Ask Questions”</em>. The bot answers the question based on the information contained in the document.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ql714BnqK7B8uRMuV_RrkQ.png" /><figcaption>Once data has been added to the KB, the bot can be tested immediately.</figcaption></figure><p>Let’s go back to the main view. An empty template currently consists of a note and the start and end points. In this example, I have also set up a simple note as a general greeting. To create a note, simply right-click on the workspace with the mouse and then select <em>“Standard Note”</em>. I added a <em>“Text”</em> card to the note in the <em>“Send Messages”</em> section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o3pj3EijMz6hazDajUlxyQ.png" /><figcaption>Before the actual question and KB query, a greeting is implemented.</figcaption></figure><p>The greeting is followed by another note. Here the card type <em>“Raw Input” </em>was selected in the <em>“Capture Information” </em>area. The previously created KB <em>“gamedevelopment”</em> is assigned to the card and a question is defined that is asked to the user. The information collection card allows the bot to pause processing and wait for responses from the user. At the same time, it captures certain information from the user input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*upFZScDdW1IKX6DnY1sYJg.png" /><figcaption>The created KB is assigned to the “Raw Input” card type and a user question is created.</figcaption></figure><p>No further settings need to be made, at least for this simple demonstration in Botpress Studio. The bot can be tried out directly. When the conversation starts, the general greeting appears first, followed by the answer to the question, which the bot can answer depending on the KB. In addition to greeting the user with text, videos, images or audio files can also be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GpzNJtMCrLqRyf4ZrjXlug.png" /><figcaption>The bot can be tested. The question is answered correctly based on the data in the KB.</figcaption></figure><h3><strong>COMPUTER SAYS NO!</strong></h3><p>What happens if the bot is asked a question that it cannot answer based on the KB’s data?</p><p>To solve the problem, we select the <em>“Raw Input”</em> card and set a variable to store the inputs. In this example, I have created a variable <em>“user_input”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tylHLfcwruylY5qAiBnOgA.png" /><figcaption>A variable “user_input” is created to save the user’s input.</figcaption></figure><p>Next, we create an <em>“Expression”</em> card, which is located in the <em>“Flow Logic” </em>area, in the current note. We select the card and define the following Boolean variable <em>“{{!turn.KnowledgeAgent.responded}}”</em> as a condition. The variable stores the information as either <em>“yes” </em>or <em>“no”</em>, or <em>“true”</em> or <em>“false”</em>. In this specific example, we tell the bot: If you cannot answer a question based on the data in the KB, you should follow the path below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KEJIpP8IwlrduAmfDgnk2Q.png" /><figcaption>If a question cannot be answered, the following path should be used.</figcaption></figure><p>Next, we create a table in which we store all the questions that the bot could not answer. In the Explorer we click on <em>“Add Table”</em>, give the table a name <em>“questions_unansweredTable”</em> and create another column (type String).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OKdzEhVhGh0fcwglS8KGQQ.png" /><figcaption>We create a table and a new column “Questions”. This collects the questions that the bot cannot answer.</figcaption></figure><p>We return to the view showing the structure of our bot. Here we create another note, connect it to the KB note and implement the <em>“Insert Record” </em>(Execute) card in it. We set the previously created table in the map. In the column (Questions) that we have created in the table, the information should be stored that is stored in the variable <em>“user_input”</em>, i.e. the input from the users “<em>{workflow.user_input}}”</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y6px_S47DNuL-c1Kdcbzpg.png" /><figcaption>The table is integrated into a new note “NO_ANSWER” and the information recorded by the “user_input” variable is stored in the “Questions” column.</figcaption></figure><p>We then create a card (text) in the note and give the user feedback that the current question cannot be answered. Now let’s test the bot again. We ask an example question that we know the bot can’t answer. The bot then gives feedback that it cannot answer this question. In the created table, the question that the bot was unable to answer is listed in the <em>“Questions” </em>column. In this way, we can quickly and easily find out how we can expand the bot’s KB or what questions users still have.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OnuciTk8G4xqThqSbUYcjQ.png" /><figcaption>The bot is asked a question that it cannot answer. The question is stored in the created table.</figcaption></figure><h3><strong>PUBLISHING THE BOT</strong></h3><p>Next, we’ll take care of publishing the bot created with Botpress. To do this in the studio, we click on <em>“Public”</em>. After publishing, we return to our dashboard by clicking on the Botpress logo in the top left. In the dashboard of the created bot, we go to <em>“Integrations”</em> and can make various settings here in the <em>“Settings”</em> area. Specifically, we can give the bot a name and store an avatar URL, if available, in order to customize the bot with its own icon. We can provide a general description of the bot, formulate a placeholder text that is displayed before the user enters their first text, as well as general information (email address, data protection information, terms &amp; conditions). After the various parameters have been edited, confirm your entries <em>“Save configuration”</em>. It will take a moment for a message to appear indicating that the customizations have been saved. To customize the color appearance and shape of the bot, we need a stylesheet URL. To generate this, we click on the word above the text field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*vGnqJncW9iOB-ydjvbu4hw.png" /><figcaption>Once the bot is created and published, it can be customized via “Integrations” in the Bot Dashboard.</figcaption></figure><h3><strong>STYLE YOUR BOT WITH WEBCHAT STYLER!</strong></h3><p>The appearance of the bot can be adjusted in the <em>“Style your Bot”</em> menu item. The bot with its standard design is shown on the right, so you can see its adjustments directly when you change the parameters. The color and size of the widget can be customized. The color of the container, the general font, the bot and user bubbles, and the shape of the chat window can be customized using the stylesheet. The <em>“Chatbot Widget Launcher Icon”</em>, i.e. the icon that is displayed before the actual chat window opens, can also be customized. For this purpose, a .png file is hosted on a server (e.g. via the media library of a WordPress site or your own Discord server) and the URL of the graphic is used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ztr5PB5koMNU7wmv0x4wvQ.png" /><figcaption>The web chat styler can be used to customize the design of the bot. In the example shown, the chatbot widget launcher icon is hosted via WordPress.</figcaption></figure><p>The bot’s icon in the chat window cannot be customized here. In the <em>“Advanced Styler”</em> menu item, the appearance of the bot can be adjusted via CSS. For example, you can remove the text in the bot’s footer by deleting the <em>“bpw-powered”</em> class. If you choose this approach, you should be familiar with CSS. The <em>“Chatbot Widget Launcher Icon” </em>can be customized with the following code:</p><pre>  /* Change Bot Widget Icon */<br>  .bpw-widget-btn{<br>    border-radius: 50%;<br>    background: url(https://medium.com/&quot;YOUR%20URL%20HERE&quot;);<br>    background-repeat: no-repeat;<br>    background-position: center;<br>    background-size: contain;<br>  }<br>  <br>  .bpw-floating-button::before {<br>    background: none; <br>  }<br>  <br>  .bpw-floating-button {<br>    box-shadow: none !important;<br>  }<br>  <br>  .bpw-floating-button:hover {<br>    box-shadow: none !important;<br>  }</pre><p>Overall, there are many more customization options available in the <em>“Advanced Styler”</em>. If the bot finally meets your own design requirements, you should save the CSS code in a .css file (style sheet) so that you don’t have to start from scratch again if you redesign it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qfVOQPG6-uOH6PduRi5E9w.png" /><figcaption>The bot’s CSS code is displayed in the Advanced Styler. Here you have more customization options overall.</figcaption></figure><p>The URL of the design “<em>Get Stylesheet URL”</em> is finally copied and placed in the dashboard of the created bot. A note: it may take a short moment for the bot to adopt the specified design after integration into WordPress, Moodle etc.</p><h3><strong>MOODLE IMPLEMENTATION</strong></h3><p>To implement the bot in an LMS like Moodle, we now use the <em>“Pre-configured”</em> option. Under the heading <em>“Embedded”</em> there are two Java scripts that we need. Specifically, the first script loads the Botpress library in order to be able to interact with the bot. The second script loads the bot’s configuration file (ID, name, appearance, behavior, etc.). Without said file, the bot would not work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EITvPTJp3a0gRCkFqO3jJQ.png" /><figcaption>To implement the bot in the Moodle LMS, the following two scripts are required.</figcaption></figure><p>As an alternative to this simple integration option, you can go to the <em>“Configurable” </em>option to adjust some of the parameters of the second script, i.e. the configuration file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SEyAFL1Vl3yd9TURrSqXTw.png" /><figcaption>A more customizable representation of the scripts required for implementation can be found in the Configurable section.</figcaption></figure><p>After the chatbot has been created and prepared using Botpress, we now move on to our Moodle installation course. In the course, we need admin rights to be able to create activities. If these rights are present, we create a simple <em>“Text and Media Field”</em> activity. In previous versions of Moodle, this activity was simply called <em>“Text Field”</em>. In the text field, we expand the menu and activate the HTML view. In the input field, we implement the two scripts that are provided for embedding on Botpress.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KM3Q4S1ngIOzWnVE82iW7A.png" /><figcaption>The scripts are integrated into a text and media field (formerly just a text field) activity in the Moodle LMS when the HTML view is activated.</figcaption></figure><p>We close the view and save the activity. We deactivate the editing of the course, after which the bot should be displayed at the bottom right of the course.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8yLxb6x-qEesF3P5PpBhcg.png" /><figcaption>The developed bot is presented in the Moodle course.</figcaption></figure><p>In addition to integrating the bot into a specific course, it can also be made available throughout the LMS. To do this, we go to the website administration and click on the <em>“Display”</em> option. In the display area we go to <em>“Additional HTML</em>”. Here we insert the two Java scripts in the <em>“additionalhtmlfooter” </em>area.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hUEPZB5hcOliq7Ag8H1y4Q.png" /><figcaption>In the website administration area of the Moodle installation, you can also integrate the scripts via additional HTML to make the bot available throughout the system.</figcaption></figure><p>The changes are saved, after which the bot is displayed throughout the LMS and is ready to answer questions.</p><h3><strong>WORDPRESS IMPLEMENTATION</strong></h3><p>The Botpress bot can also be integrated into WordPress sites in various ways. The easiest option is to use the WPCode plugin, which can be used to integrate code or scripts into the header or footer area of the page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CUmzQO4LHIvT-zJG4yqclw.png" /><figcaption>Specific plugins are required to implement the bot in a WordPress site. Alternatively, you need access to the system files to edit the backend.</figcaption></figure><p>Another option is to integrate the scripts directly via the theme file. Both variants require the <em>“Creator”</em> tariff. which allows you to install plugins and edit certain areas. The two Java scripts cannot be integrated via the code editor or a <em>“custom HTML” </em>block because WordPress automatically interprets the HTML code as a link or deactivates certain types of JavaScript for security reasons.</p><h3>SUMMARY</h3><p>This post showed step by step how to use Botpress to create a bot that can answer questions based on a self-defined KB. At the same time, the questions that cannot be answered are collected to improve the bot. It was then shown how the design of the bot can be individualized. Finally, it was explained how the bot can be implemented in the Moodle LMS. Possible ways in which the bot can be integrated into a site created using WordPress were also shown. The bot created is a simple example of answering specific questions relating to a seminar. Of course, Botpress Studio can also be used to create more complex bots that can generally support learners in their learning. A recent publication shows that studies on chatbots in education are still at an early stage. Overall, there are too few studies that examine the use of effective learning strategies with chatbots [1]. Overall, it is an interesting field of research. Have fun developing your own bot!</p><p>[1] Hwang, G. J., &amp; Chang, C. Y. (2023). A review of opportunities and challenges of chatbots in education. <em>Interactive Learning Environments</em>, <em>31</em>(7), 4099–4112. <a href="https://doi.org/10.1080/10494820.2021.1952615">https://doi.org/10.1080/10494820.2021.1952615</a></p><p>Click here to go to the GitHub main page of Botpress <a href="https://github.com/botpress">https://github.com/botpress</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R5nZEjKUHA1qcgyzXTJ5yQ.png" /><figcaption>Just for the thumbnail (Image by author)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55d3a46cbaff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>