CoolFace
Apppublic

darwisht/apollo

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
bot.ipynb346 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 5,6   "id": "053b7b64",7   "metadata": {},8   "outputs": [9    {10     "name": "stderr",11     "output_type": "stream",12     "text": [13      "[nltk_data] Downloading package averaged_perceptron_tagger to\n",14      "[nltk_data]     /Users/darwisht/nltk_data...\n",15      "[nltk_data]   Package averaged_perceptron_tagger is already up-to-\n",16      "[nltk_data]       date!\n",17      "[nltk_data] Downloading package punkt to /Users/darwisht/nltk_data...\n",18      "[nltk_data]   Package punkt is already up-to-date!\n",19      "[nltk_data] Downloading package stopwords to\n",20      "[nltk_data]     /Users/darwisht/nltk_data...\n",21      "[nltk_data]   Package stopwords is already up-to-date!\n"22     ]23    },24    {25     "name": "stdout",26     "output_type": "stream",27     "text": [28      "User: i feel slight fever and have some rashes\n",29      "Bot: Thank you for sharing your symptoms. To provide a more accurate diagnosis, I would like to gather some additional information. Please answer the following questions:\n",30      "\n",31      "1. On a scale of 1 to 10, how would you rate the severity of your fever?\n",32      "2. Have you taken your temperature? If so, what is your current body temperature?\n",33      "3. Where are the rashes located on your body? Are they itchy or painful?\n",34      "4. When did you first notice the fever and rashes?\n",35      "5. Have you recently been in contact with someone who has a fever or a rash?\n",36      "6. Are you experiencing any other symptoms such as cough, sore throat, headache, or body aches?\n",37      "7. Have you traveled recently, especially to any areas with known infectious diseases?\n",38      "8. Have you been vaccinated against common infectious diseases?\n",39      "9. Do you have any pre-existing medical conditions or take any medications regularly?\n",40      "\n",41      "Please provide as much detail as possible so that I can assist you further.\n",42      "User: bye\n"43     ]44    }45   ],46   "source": [47    "from chatterbot import ChatBot\n",48    "import openai\n",49    "\n",50    "openai.api_key = \"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\"\n",51    "\n",52    "chatbot = ChatBot(\"Apollo\")\n",53    "messages = [\n",54    "    {\"role\": \"system\", \"content\": \"You are a helpful medical assistant specialized in diagnosing symptoms. Prompt for more detailed descriptions of symptoms to reach a medical diagnosis\"}\n",55    "]\n",56    "\n",57    "exit_conditions = (\"bye\", \"quit\", \"exit\", \"Bye\")\n",58    "\n",59    "def process_query(query):\n",60    "    global messages\n",61    "    messages.append({\"role\": \"user\", \"content\": query})\n",62    "    response = ChatCompletion.create(\n",63    "        model=\"gpt-3.5-turbo\",\n",64    "        messages=messages,\n",65    "        n=1,\n",66    "        stop=None,\n",67    "        temperature=0.2,\n",68    "    )\n",69    "    reply = response.choices[0].message.content.strip()\n",70    "    messages.append({\"role\": \"assistant\", \"content\": reply})\n",71    "    return reply\n",72    "\n",73    "while True:\n",74    "    user_input = input(\"User: \")\n",75    "    if user_input in exit_conditions:\n",76    "        break\n",77    "    else:\n",78    "        bot_response = process_query(user_input)\n",79    "        print(\"Bot:\", bot_response)\n"80   ]81  },82  {83   "cell_type": "code",84   "execution_count": 24,85   "id": "6b3c685b",86   "metadata": {},87   "outputs": [88    {89     "name": "stderr",90     "output_type": "stream",91     "text": [92      "[nltk_data] Downloading package averaged_perceptron_tagger to\n",93      "[nltk_data]     /Users/darwisht/nltk_data...\n",94      "[nltk_data]   Package averaged_perceptron_tagger is already up-to-\n",95      "[nltk_data]       date!\n",96      "[nltk_data] Downloading package punkt to /Users/darwisht/nltk_data...\n",97      "[nltk_data]   Package punkt is already up-to-date!\n",98      "[nltk_data] Downloading package stopwords to\n",99      "[nltk_data]     /Users/darwisht/nltk_data...\n",100      "[nltk_data]   Package stopwords is already up-to-date!\n"101     ]102    },103    {104     "name": "stdout",105     "output_type": "stream",106     "text": [107      "Running on local URL:  http://127.0.0.1:7865\n",108      "\n",109      "To create a public link, set `share=True` in `launch()`.\n"110     ]111    },112    {113     "data": {114      "text/html": [115       "<div><iframe src=\"http://127.0.0.1:7865/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"116      ],117      "text/plain": [118       "<IPython.core.display.HTML object>"119      ]120     },121     "metadata": {},122     "output_type": "display_data"123    },124    {125     "data": {126      "text/plain": []127     },128     "execution_count": 24,129     "metadata": {},130     "output_type": "execute_result"131    }132   ],133   "source": [134    "import gradio as gr\n",135    "from chatterbot import ChatBot\n",136    "import openai\n",137    "\n",138    "openai.api_key = \"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\"\n",139    "\n",140    "chatbot = ChatBot(\"Apollo\")\n",141    "messages = []\n",142    "\n",143    "exit_conditions = (\"bye\", \"quit\", \"exit\", \"Bye\")\n",144    "\n",145    "def process_query(query):\n",146    "    global messages\n",147    "    messages.append({\"role\": \"user\", \"content\": query})\n",148    "    response = ChatCompletion.create(\n",149    "        model=\"gpt-3.5-turbo\",\n",150    "        messages=messages,\n",151    "        n=1,\n",152    "        stop=None,\n",153    "        temperature=0.2,\n",154    "    )\n",155    "    reply = response.choices[0].message.content.strip()\n",156    "    messages.append({\"role\": \"assistant\", \"content\": reply})\n",157    "    return reply\n",158    "\n",159    "def chat_interface(query):\n",160    "    if query in exit_conditions:\n",161    "        return \"Exiting...\"\n",162    "    else:\n",163    "        bot_response = process_query(query)\n",164    "        return bot_response\n",165    "    \n",166    "iface = gr.Interface(fn=chat_interface, inputs=\"text\", outputs=\"text\", title=\"Symptom Analyser\")\n",167    "iface.launch()"168   ]169  },170  {171   "cell_type": "code",172   "execution_count": 23,173   "id": "7879cc1a",174   "metadata": {},175   "outputs": [176    {177     "name": "stdout",178     "output_type": "stream",179     "text": [180      "\n",181      "        <div id=\"chatbot-container\">\n",182      "            <div id=\"chatbot-interface\"></div>\n",183      "            <script src=\"https://cdn.jsdelivr.net/npm/gradio\"></script>\n",184      "            <script>\n",185      "                const chatbotInterface = gradio.Interface(\n",186      "                    chat_interface,\n",187      "                    gr.inputs.text(),\n",188      "                    gr.outputs.text(),\n",189      "                    {\"title\": \"Symptom Analyser\"}\n",190      "                );\n",191      "                chatbotInterface.enableInput();\n",192      "                chatbotInterface.enableOutput();\n",193      "                chatbotInterface.render(document.getElementById(\"chatbot-interface\"));\n",194      "            </script>\n",195      "        </div>\n",196      "    \n"197     ]198    },199    {200     "name": "stderr",201     "output_type": "stream",202     "text": [203      "[nltk_data] Downloading package averaged_perceptron_tagger to\n",204      "[nltk_data]     /Users/darwisht/nltk_data...\n",205      "[nltk_data]   Package averaged_perceptron_tagger is already up-to-\n",206      "[nltk_data]       date!\n",207      "[nltk_data] Downloading package punkt to /Users/darwisht/nltk_data...\n",208      "[nltk_data]   Package punkt is already up-to-date!\n",209      "[nltk_data] Downloading package stopwords to\n",210      "[nltk_data]     /Users/darwisht/nltk_data...\n",211      "[nltk_data]   Package stopwords is already up-to-date!\n"212     ]213    }214   ],215   "source": [216    "import gradio as gr\n",217    "from chatterbot import ChatBot\n",218    "from openai import ChatCompletion\n",219    "\n",220    "chatbot = ChatBot(\"Apollo\")\n",221    "messages = []\n",222    "\n",223    "exit_conditions = (\"bye\", \"quit\", \"exit\", \"Bye\")\n",224    "\n",225    "def process_query(query):\n",226    "    global messages\n",227    "    messages.append({\"role\": \"user\", \"content\": query})\n",228    "    response = ChatCompletion.create(\n",229    "        model=\"gpt-3.5-turbo\",\n",230    "        messages=messages,\n",231    "        n=1,\n",232    "        stop=None,\n",233    "        temperature=0.2,\n",234    "    )\n",235    "    reply = response.choices[0].message.content.strip()\n",236    "    messages.append({\"role\": \"assistant\", \"content\": reply})\n",237    "    return reply\n",238    "\n",239    "def chat_interface(query):\n",240    "    if query in exit_conditions:\n",241    "        return \"Exiting...\"\n",242    "    else:\n",243    "        bot_response = process_query(query)\n",244    "        return bot_response\n",245    "\n",246    "def generate_html():\n",247    "    return f'''\n",248    "        <div id=\"chatbot-container\">\n",249    "            <div id=\"chatbot-interface\"></div>\n",250    "            <script src=\"https://cdn.jsdelivr.net/npm/gradio\"></script>\n",251    "            <script>\n",252    "                const chatbotInterface = gradio.Interface(\n",253    "                    chat_interface,\n",254    "                    gr.inputs.text(),\n",255    "                    gr.outputs.text(),\n",256    "                    {{\"title\": \"Symptom Analyser\"}}\n",257    "                );\n",258    "                chatbotInterface.enableInput();\n",259    "                chatbotInterface.enableOutput();\n",260    "                chatbotInterface.render(document.getElementById(\"chatbot-interface\"));\n",261    "            </script>\n",262    "        </div>\n",263    "    '''\n",264    "\n",265    "html_code = generate_html()\n",266    "\n",267    "# Print or save the HTML code\n",268    "print(html_code)\n",269    "# Or write it to a file\n",270    "# with open(\"chatbot.html\", \"w\") as f:\n",271    "#     f.write(html_code)\n"272   ]273  },274  {275   "cell_type": "code",276   "execution_count": 25,277   "id": "c5b9f733",278   "metadata": {},279   "outputs": [280    {281     "ename": "SyntaxError",282     "evalue": "invalid syntax (1241365062.py, line 1)",283     "output_type": "error",284     "traceback": [285      "\u001b[0;36m  Input \u001b[0;32mIn [25]\u001b[0;36m\u001b[0m\n\u001b[0;31m    python version\u001b[0m\n\u001b[0m           ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"286     ]287    }288   ],289   "source": [290    "python version"291   ]292  },293  {294   "cell_type": "code",295   "execution_count": 26,296   "id": "66e668bb",297   "metadata": {},298   "outputs": [299    {300     "ename": "NameError",301     "evalue": "name 'python' is not defined",302     "output_type": "error",303     "traceback": [304      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",305      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",306      "Input \u001b[0;32mIn [26]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mpython\u001b[49m \u001b[38;5;241m-\u001b[39m version\n",307      "\u001b[0;31mNameError\u001b[0m: name 'python' is not defined"308     ]309    }310   ],311   "source": [312    "python - version"313   ]314  },315  {316   "cell_type": "code",317   "execution_count": null,318   "id": "c591e195",319   "metadata": {},320   "outputs": [],321   "source": []322  }323 ],324 "metadata": {325  "kernelspec": {326   "display_name": "Python 3 (ipykernel)",327   "language": "python",328   "name": "python3"329  },330  "language_info": {331   "codemirror_mode": {332    "name": "ipython",333    "version": 3334   },335   "file_extension": ".py",336   "mimetype": "text/x-python",337   "name": "python",338   "nbconvert_exporter": "python",339   "pygments_lexer": "ipython3",340   "version": "3.9.13"341  }342 },343 "nbformat": 4,344 "nbformat_minor": 5345}346