{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install transformers gradio","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load model directly\nfrom transformers import AutoTokenizer, AutoModelForCausalLM\n\ntokenizer = AutoTokenizer.from_pretrained(\"Intel/neural-chat-7b-v3-1\")\nmodel = AutoModelForCausalLM.from_pretrained(\"Intel/neural-chat-7b-v3-1\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_response(system_input, user_input):\n\n # Format the input using the provided template\n prompt = f\"### System:\\n{system_input}\\n### User:\\n{user_input}\\n### Assistant:\\n\"\n\n # Tokenize and encode the prompt\n inputs = tokenizer.encode(prompt, return_tensors=\"pt\", add_special_tokens=False)\n\n # Generate a response\n outputs = model.generate(inputs, max_length=1000, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)\n \n response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n\n # Extract only the assistant's response\n return response.split(\"### Assistant:\\n\")[-1]","metadata":{"execution":{"iopub.status.busy":"2023-12-13T16:35:31.082768Z","iopub.execute_input":"2023-12-13T16:35:31.083305Z","iopub.status.idle":"2023-12-13T16:35:31.088838Z","shell.execute_reply.started":"2023-12-13T16:35:31.083270Z","shell.execute_reply":"2023-12-13T16:35:31.088184Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"'''# Example usage\nsystem_input = \"Please act as a psychology counselor assistant. I will provide you counselling notes and you need to provide me exactly two things in return, assessment and plan for the notes. Please note that the assessment should begin with keword 'Assessment:', and plan should begin with keyword 'Plan:'. Please make it logical, simple, concise, and clear.\"\nuser_input = \"Can't sleep well?\"\nresponse = generate_response(system_input, user_input)\nprint(response)\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_response1(user_input):\n system_input = \"Please act as a psychology counselor assistant. I will provide you counselling notes and you need to provide me exactly two things in return, assessment and plan for the notes. Please note that the assessment should begin with keword 'Assessment:', and plan should begin with keyword 'Plan:'. Please make it logical, simple, concise, and clear.\"\n prompt = f\"### System:\\n{system_input}\\n### User:\\n{user_input}\\n### Assistant:\\n\"\n inputs = tokenizer.encode(prompt, return_tensors=\"pt\", add_special_tokens=False)\n outputs = model.generate(inputs, max_length=1000, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)\n response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n response_parts = response.split(\"### Assistant:\\n\")[-1]\n \n # Split the response into Assessment and Plan\n assessment = response_parts.split(\"Plan:\")[0].strip()\n plan = \"Plan:\" + response_parts.split(\"Plan:\")[1].strip()\n\n return assessment, plan","metadata":{"execution":{"iopub.status.busy":"2023-12-13T17:14:11.817666Z","iopub.execute_input":"2023-12-13T17:14:11.817979Z","iopub.status.idle":"2023-12-13T17:14:11.824301Z","shell.execute_reply.started":"2023-12-13T17:14:11.817938Z","shell.execute_reply":"2023-12-13T17:14:11.823487Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"import gradio as gr\n# Gradio interface\niface = gr.Interface(\n fn=generate_response1,\n inputs=gr.Textbox(lines=2, label=\"Please Upload Notes here\", placeholder=\"Enter your notes here...\"),\n outputs=[gr.Textbox(label=\"Assessment\"), gr.Textbox(label=\"Plan\")],\n examples=[\n [\"Can't sleep well?\"],\n [\"Feeling anxious all the time.\"]\n ]\n)\n\n# Launch the interface\niface.launch(share=True)","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]}]}