Spaces:
Runtime error
Runtime error
File size: 5,627 Bytes
59c3706 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9CrIbR0AK3d1",
"outputId": "8624a380-d370-43b0-969c-1b21e275b322"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: typing_extensions in /usr/local/lib/python3.10/dist-packages (4.9.0)\n"
]
}
],
"source": [
"# !pip install openai sentence-transformers\n",
"# !pip install langchain\n",
"!pip install typing_extensions\n"
]
},
{
"cell_type": "code",
"source": [
"import os\n",
"import openai\n",
"from langchain_community.document_loaders import TextLoader, PyPDFLoader, CSVLoader, DirectoryLoader\n",
"from transformers import AutoModel\n",
"from langchain_community.embeddings.sentence_transformer import (\n",
" SentenceTransformerEmbeddings,\n",
")\n",
"from langchain_community.vectorstores import Chroma\n",
"import torch\n",
"import json"
],
"metadata": {
"id": "xOFM83MoLQ-B"
},
"execution_count": 20,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('new_articles')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WMvNDl83M7Xb",
"outputId": "d59ab804-42ce-4b10-fee6-f01f19d60b38"
},
"execution_count": 53,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at new_articles; to attempt to forcibly remount, call drive.mount(\"new_articles\", force_remount=True).\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def document_loader(directory):\n",
" documents = {}\n",
" for filename in os.listdir(directory):\n",
" file_path = os.path.join(directory, filename)\n",
" if filename.endswith(\".csv\"):\n",
" loader = CSVLoader(file_path)\n",
" elif filename.endswith(\".pdf\"):\n",
" loader = PyPDFLoader(file_path)\n",
" elif filename.endswith(\".txt\"):\n",
" loader = TextLoader(file_path)\n",
" else:\n",
" break\n",
"\n",
" document = loader.load()\n",
" documents[filename] = document\n",
" return (documents)\n"
],
"metadata": {
"id": "QxVY8IyNL3Zp"
},
"execution_count": 54,
"outputs": []
},
{
"cell_type": "code",
"source": [
"openai.api_key = \"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\"\n",
"JSON_DATA = []\n",
"directory = \"/content/new_articles/MyDrive/new_articles\"\n",
"documents = document_loader(directory)\n",
"for filename, document in documents.items():\n",
" doc = document[0].page_content\n",
" # print(filename)\n",
" # print(document)\n",
" response = openai.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": f\"Generate one Question, Answer,Reference_Article:(use {filename}), Reference_Text from(use block of text which you've used to generate answer {doc})\"},\n",
" ], temperature = 0.3\n",
" )\n",
" #print(response)\n",
" result = response.choices[0].message.content.split(\"\\n\")\n",
" # print(result)\n",
" json_data = {\n",
" \"Question\": result[0].split(\"Question: \")[1].strip() if len(result) > 0 and \"Question:\" in result[0] else \"Not provided\",\n",
" \"Answer\": result[2].split(\"Answer: \")[1].strip() if len(result) > 2 and \"Answer:\" in result[2] else \"Not provided\",\n",
" \"Reference_article\": result[4].split(\"Reference_article: \")[1].strip() if len(result) > 4 and \"Reference_article:\" in result[4] else \"Not provided\",\n",
" \"Reference_text\": result[6].split(\"Reference_text: \")[1].strip() if len(result) > 6 and \"Reference_text:\" in result[6] else \"Not provided\",\n",
" }\n",
"\n",
" # print(json_data)\n",
"\n",
" JSON_DATA.append(json_data)\n",
"\n",
"with open('question_and_answer_list.json', 'w') as json_file:\n",
" json.dump(JSON_DATA, json_file, indent=2)\n",
"\n",
"print(\"JSON data saved to question_and_answer_list.json\")\n",
"\n",
"print(JSON_DATA)\n"
],
"metadata": {
"id": "LO9imR5SMA1u"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "eOAr3cy6iA9J"
},
"execution_count": 46,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "E86P5xBqizsG"
},
"execution_count": null,
"outputs": []
}
]
} |