Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ from flask import Flask, render_template, request, jsonify
|
|
2 |
import subprocess
|
3 |
import tempfile
|
4 |
import os
|
5 |
-
from
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from langchain.chains import LLMChain
|
8 |
from langchain.memory import ConversationBufferMemory
|
@@ -13,6 +13,8 @@ import sqlite3
|
|
13 |
from contextlib import contextmanager
|
14 |
import re
|
15 |
from werkzeug.utils import secure_filename
|
|
|
|
|
16 |
|
17 |
app = Flask(__name__)
|
18 |
|
@@ -32,27 +34,27 @@ import torch
|
|
32 |
# Load model and tokenizer
|
33 |
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
|
34 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
35 |
-
model = AutoModelForCausalLM.from_pretrained(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
llm =
|
56 |
|
57 |
@contextmanager
|
58 |
def get_db_connection():
|
|
|
2 |
import subprocess
|
3 |
import tempfile
|
4 |
import os
|
5 |
+
from langchain_community.llms import HuggingFacePipeline
|
6 |
from langchain.prompts import PromptTemplate
|
7 |
from langchain.chains import LLMChain
|
8 |
from langchain.memory import ConversationBufferMemory
|
|
|
13 |
from contextlib import contextmanager
|
14 |
import re
|
15 |
from werkzeug.utils import secure_filename
|
16 |
+
import torch
|
17 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
18 |
|
19 |
app = Flask(__name__)
|
20 |
|
|
|
34 |
# Load model and tokenizer
|
35 |
model_name = "mistralai/Mistral-7B-Instruct-v0.1"
|
36 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
37 |
+
model = AutoModelForCausalLM.from_pretrained(
|
38 |
+
model_name,
|
39 |
+
torch_dtype=torch.float16,
|
40 |
+
device_map="auto",
|
41 |
+
load_in_8bit=True
|
42 |
+
)
|
43 |
+
|
44 |
+
# Create pipeline
|
45 |
+
pipe = pipeline(
|
46 |
+
"text-generation",
|
47 |
+
model=model,
|
48 |
+
tokenizer=tokenizer,
|
49 |
+
max_new_tokens=512,
|
50 |
+
temperature=0.7,
|
51 |
+
top_p=0.95,
|
52 |
+
repetition_penalty=1.15
|
53 |
+
)
|
54 |
+
|
55 |
+
|
56 |
|
57 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
58 |
|
59 |
@contextmanager
|
60 |
def get_db_connection():
|