Spaces:
Runtime error
Runtime error
Upto12forenglish
commited on
Commit
•
6d495b4
1
Parent(s):
871afa6
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,56 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
|
5 |
import transformers
|
6 |
import torch
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
|
11 |
-
|
|
|
12 |
|
|
|
13 |
pipeline = transformers.pipeline(
|
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 |
-
print(
|
|
|
1 |
+
#Loading the HF_TOKEN from the .env file
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
load_dotenv()
|
4 |
|
5 |
import transformers
|
6 |
import torch
|
7 |
+
from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
|
8 |
|
9 |
+
#Loading llama3 model
|
10 |
+
local_model_path = "meta-llama\\Meta-Llama-3-8B-Instruct"
|
11 |
|
12 |
+
model = transformers.AutoModelForCausalLM.from_pretrained(local_model_path, torch_dtype=torch.bfloat16)
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(local_model_path, padding_side='left')
|
14 |
|
15 |
+
# Set up the pipeline
|
16 |
pipeline = transformers.pipeline(
|
17 |
+
"text-generation",
|
18 |
+
model=model,
|
19 |
+
tokenizer=tokenizer,
|
20 |
+
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
|
21 |
)
|
22 |
|
23 |
+
def chat_function(message, history, system_prompt,max_new_tokens,temperature):
|
24 |
+
messages = [
|
25 |
+
{"role": "system", "content": system_prompt},
|
26 |
+
{"role": "user", "content": message},
|
27 |
+
]
|
28 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
29 |
+
messages,
|
30 |
+
tokenize=False,
|
31 |
+
add_generation_prompt=True
|
32 |
+
)
|
33 |
+
terminators = [
|
34 |
+
pipeline.tokenizer.eos_token_id,
|
35 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
36 |
+
]
|
37 |
+
temp = temperature + 0.1
|
38 |
+
outputs = pipeline(
|
39 |
+
prompt,
|
40 |
+
max_new_tokens=max_new_tokens,
|
41 |
+
eos_token_id=terminators,
|
42 |
+
do_sample=True,
|
43 |
+
temperature=temp,
|
44 |
+
top_p=0.9,
|
45 |
+
)
|
46 |
+
return outputs[0]["generated_text"][len(prompt):]
|
47 |
+
|
48 |
+
message = "Hello, can you teach me past simple?"
|
49 |
+
history = [("Hi!", "I'm doing well, thanks for asking!")]
|
50 |
+
temperature = 0.7
|
51 |
+
max_new_tokens = 50
|
52 |
+
prompt = "Act as an english tutor. Always correct grammar and spelling mistakes. Always keep the conversation going by asking follow up questions"
|
53 |
+
|
54 |
+
response = chat_function(message=message, history= history, system_prompt= prompt, max_new_tokens= max_new_tokens, temperature= temperature)
|
55 |
+
|
56 |
+
print(response)
|