turkfork commited on
Commit
0b4af66
·
verified ·
1 Parent(s): d483e7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -5,25 +5,23 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
5
  model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
6
  tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
7
 
8
- # Chatbot function with improved tokenization and generation
9
- def chatbot(input_text):
10
- # Tokenize input with padding and truncation
11
- inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
12
- input_ids = inputs["input_ids"]
13
- attention_mask = inputs["attention_mask"]
14
 
15
- # Generate response
 
 
 
 
 
 
 
16
  outputs = model.generate(
17
- input_ids=input_ids,
18
- attention_mask=attention_mask,
19
  max_length=200,
20
- pad_token_id=tokenizer.eos_token_id, # Avoids padding issues
21
  do_sample=True,
22
- temperature=0.7, # Slight randomness
23
- top_p=0.9 # Nucleus sampling for more creative replies
24
  )
25
-
26
- # Decode and return response
27
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
  return response
29
 
@@ -33,9 +31,8 @@ iface = gr.Interface(
33
  inputs="text",
34
  outputs="text",
35
  title="AeroAI Chatbot (Phi-2)",
36
- description="Chat with AeroAI powered by Phi-2! Created by Blacklink Education, LLC & Microsoft Corporation"
37
  )
38
 
39
  # Launch the interface
40
  iface.launch()
41
-
 
5
  model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
6
  tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
7
 
8
+ # Set pad_token as eos_token
9
+ tokenizer.pad_token = tokenizer.eos_token
 
 
 
 
10
 
11
+ # Chatbot function
12
+ def chatbot(input_text):
13
+ inputs = tokenizer(
14
+ input_text,
15
+ return_tensors="pt",
16
+ padding=True,
17
+ truncation=True
18
+ )
19
  outputs = model.generate(
20
+ **inputs,
 
21
  max_length=200,
 
22
  do_sample=True,
23
+ pad_token_id=tokenizer.pad_token_id
 
24
  )
 
 
25
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
26
  return response
27
 
 
31
  inputs="text",
32
  outputs="text",
33
  title="AeroAI Chatbot (Phi-2)",
34
+ description="Chat with AeroAI powered by Phi-2!"
35
  )
36
 
37
  # Launch the interface
38
  iface.launch()