Upload model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Mistral-7B.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1jBeeSsVx1bnTm2GM-0WRS4TF24Ayb6rw
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from huggingface_hub import InferenceClient
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
client = InferenceClient(
|
| 14 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def format_prompt(message, history):
|
| 18 |
+
prompt = "<s>"
|
| 19 |
+
for user_prompt, bot_response in history:
|
| 20 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 21 |
+
prompt += f" {bot_response}</s> "
|
| 22 |
+
prompt += f"[INST] {message} [/INST]"
|
| 23 |
+
return prompt
|
| 24 |
+
|
| 25 |
+
def generate(
|
| 26 |
+
prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
| 27 |
+
):
|
| 28 |
+
temperature = float(temperature)
|
| 29 |
+
if temperature < 1e-2:
|
| 30 |
+
temperature = 1e-2
|
| 31 |
+
top_p = float(top_p)
|
| 32 |
+
|
| 33 |
+
generate_kwargs = dict(
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
max_new_tokens=max_new_tokens,
|
| 36 |
+
top_p=top_p,
|
| 37 |
+
repetition_penalty=repetition_penalty,
|
| 38 |
+
do_sample=True,
|
| 39 |
+
seed=42,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Check if the history is empty. If so, prepend the system prompt.
|
| 43 |
+
if not history:
|
| 44 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 45 |
+
else:
|
| 46 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 47 |
+
|
| 48 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 49 |
+
output = ""
|
| 50 |
+
|
| 51 |
+
for response in stream:
|
| 52 |
+
output += response.token.text
|
| 53 |
+
yield output
|
| 54 |
+
return output
|
| 55 |
+
|
| 56 |
+
additional_inputs=[
|
| 57 |
+
gr.Textbox(
|
| 58 |
+
label="System Prompt",
|
| 59 |
+
max_lines=1,
|
| 60 |
+
interactive=True,
|
| 61 |
+
)
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
examples=[
|
| 65 |
+
["Can you explain the concept of cognitive dissonance and how it affects decision-making?", None, None, None, None, None],
|
| 66 |
+
["What are some effective psychotherapy techniques for managing anxiety?", None, None, None, None, None],
|
| 67 |
+
["Can you provide an overview of the stages of development according to Erik Erikson's theory?", None, None, None, None, None],
|
| 68 |
+
["How does the human memory work and what factors can influence our ability to recall information?", None, None, None, None, None],
|
| 69 |
+
["What are the psychological impacts of social media on teenagers?", None, None, None, None, None],
|
| 70 |
+
["Can you explain the difference between classical and operant conditioning in behavioral psychology?", None, None, None, None, None]
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
gr.ChatInterface(
|
| 74 |
+
fn=generate,
|
| 75 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 76 |
+
additional_inputs=additional_inputs,
|
| 77 |
+
title="PsyAssist - ADVANCING MENTAL HEALTH SUPPORT WITH AI-DRIVEN INTERACTION",
|
| 78 |
+
examples=examples,
|
| 79 |
+
concurrency_limit=20,
|
| 80 |
+
).launch(show_api=False)
|