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_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from transformers import AutoTokenizer\n",
|
| 10 |
+
"import transformers\n",
|
| 11 |
+
"import torch\n",
|
| 12 |
+
"\n",
|
| 13 |
+
"model = \"meta-llama/Llama-2-7b-chat-hf\" # meta-llama/Llama-2-7b-chat-hf\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"tokenizer = AutoTokenizer.from_pretrained(model, use_auth_token=True)"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"cell_type": "code",
|
| 20 |
+
"execution_count": null,
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"outputs": [],
|
| 23 |
+
"source": [
|
| 24 |
+
"from transformers import pipeline\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"llama_pipeline = pipeline(\n",
|
| 27 |
+
" \"text-generation\", # LLM task\n",
|
| 28 |
+
" model=model,\n",
|
| 29 |
+
" torch_dtype=torch.float16,\n",
|
| 30 |
+
" device_map=\"auto\",\n",
|
| 31 |
+
")"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": null,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"outputs": [],
|
| 39 |
+
"source": [
|
| 40 |
+
"SYSTEM_PROMPT = \"\"\"<s>[INST] <<SYS>>\n",
|
| 41 |
+
"You are a helpful bot. Your answers are clear and concise.\n",
|
| 42 |
+
"<</SYS>>\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"\"\"\"\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"# Formatting function for message and history\n",
|
| 47 |
+
"def format_message(message: str, history: list, memory_limit: int = 3) -> str:\n",
|
| 48 |
+
" \"\"\"\n",
|
| 49 |
+
" Formats the message and history for the Llama model.\n",
|
| 50 |
+
"\n",
|
| 51 |
+
" Parameters:\n",
|
| 52 |
+
" message (str): Current message to send.\n",
|
| 53 |
+
" history (list): Past conversation history.\n",
|
| 54 |
+
" memory_limit (int): Limit on how many past interactions to consider.\n",
|
| 55 |
+
"\n",
|
| 56 |
+
" Returns:\n",
|
| 57 |
+
" str: Formatted message string\n",
|
| 58 |
+
" \"\"\"\n",
|
| 59 |
+
" # always keep len(history) <= memory_limit\n",
|
| 60 |
+
" if len(history) > memory_limit:\n",
|
| 61 |
+
" history = history[-memory_limit:]\n",
|
| 62 |
+
"\n",
|
| 63 |
+
" if len(history) == 0:\n",
|
| 64 |
+
" return SYSTEM_PROMPT + f\"{message} [/INST]\"\n",
|
| 65 |
+
"\n",
|
| 66 |
+
" formatted_message = SYSTEM_PROMPT + f\"{history[0][0]} [/INST] {history[0][1]} </s>\"\n",
|
| 67 |
+
"\n",
|
| 68 |
+
" # Handle conversation history\n",
|
| 69 |
+
" for user_msg, model_answer in history[1:]:\n",
|
| 70 |
+
" formatted_message += f\"<s>[INST] {user_msg} [/INST] {model_answer} </s>\"\n",
|
| 71 |
+
"\n",
|
| 72 |
+
" # Handle the current message\n",
|
| 73 |
+
" formatted_message += f\"<s>[INST] {message} [/INST]\"\n",
|
| 74 |
+
"\n",
|
| 75 |
+
" return formatted_message"
|
| 76 |
+
]
|
| 77 |
+
},
|
| 78 |
+
{
|
| 79 |
+
"cell_type": "code",
|
| 80 |
+
"execution_count": null,
|
| 81 |
+
"metadata": {},
|
| 82 |
+
"outputs": [],
|
| 83 |
+
"source": [
|
| 84 |
+
"# Generate a response from the Llama model\n",
|
| 85 |
+
"def get_llama_response(message: str, history: list) -> str:\n",
|
| 86 |
+
" \"\"\"\n",
|
| 87 |
+
" Generates a conversational response from the Llama model.\n",
|
| 88 |
+
"\n",
|
| 89 |
+
" Parameters:\n",
|
| 90 |
+
" message (str): User's input message.\n",
|
| 91 |
+
" history (list): Past conversation history.\n",
|
| 92 |
+
"\n",
|
| 93 |
+
" Returns:\n",
|
| 94 |
+
" str: Generated response from the Llama model.\n",
|
| 95 |
+
" \"\"\"\n",
|
| 96 |
+
" query = format_message(message, history)\n",
|
| 97 |
+
" response = \"\"\n",
|
| 98 |
+
"\n",
|
| 99 |
+
" sequences = llama_pipeline(\n",
|
| 100 |
+
" query,\n",
|
| 101 |
+
" do_sample=True,\n",
|
| 102 |
+
" top_k=10,\n",
|
| 103 |
+
" num_return_sequences=1,\n",
|
| 104 |
+
" eos_token_id=tokenizer.eos_token_id,\n",
|
| 105 |
+
" max_length=1024,\n",
|
| 106 |
+
" )\n",
|
| 107 |
+
"\n",
|
| 108 |
+
" generated_text = sequences[0]['generated_text']\n",
|
| 109 |
+
" response = generated_text[len(query):] # Remove the prompt from the output\n",
|
| 110 |
+
"\n",
|
| 111 |
+
" print(\"Chatbot:\", response.strip())\n",
|
| 112 |
+
" return response.strip()"
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"cell_type": "code",
|
| 117 |
+
"execution_count": null,
|
| 118 |
+
"metadata": {},
|
| 119 |
+
"outputs": [],
|
| 120 |
+
"source": [
|
| 121 |
+
"import gradio as gr\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"gr.ChatInterface(get_llama_response).launch()\n"
|
| 124 |
+
]
|
| 125 |
+
}
|
| 126 |
+
],
|
| 127 |
+
"metadata": {
|
| 128 |
+
"kernelspec": {
|
| 129 |
+
"display_name": "itam",
|
| 130 |
+
"language": "python",
|
| 131 |
+
"name": "python3"
|
| 132 |
+
},
|
| 133 |
+
"language_info": {
|
| 134 |
+
"name": "python",
|
| 135 |
+
"version": "3.11.3"
|
| 136 |
+
}
|
| 137 |
+
},
|
| 138 |
+
"nbformat": 4,
|
| 139 |
+
"nbformat_minor": 2
|
| 140 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
|
| 3 |
+
# gr.ChatInterface(get_llama_response).launch()
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
def greet(name):
|
| 8 |
+
return "Hello " + name + "!!"
|
| 9 |
+
|
| 10 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 11 |
+
iface.launch()
|