hosseinhimself commited on
Commit
c62d678
·
verified ·
1 Parent(s): 5d8e9c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -8,8 +8,6 @@ device = torch.device("cpu") # Ensure it's using CPU only
8
  # Load model and tokenizer
9
  model_name = "hosseinhimself/ISANG-v1.0-8B" # Replace with your model name
10
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
11
-
12
- # Load the model for inference on CPU
13
  model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
14
 
15
  # Define the Alpaca-style prompt template
@@ -24,23 +22,29 @@ You are ISANG, a multilingual large language model made by ISANG AI. You only re
24
  ### Response:
25
  {}"""
26
 
27
- EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
28
-
29
- # Define a function to generate responses
30
  def generate_response(input_text, max_tokens=1024, temperature=0.7, history=[]):
31
- # Prepare the inputs for the model with the history
32
- prompt = f"Chat History: {history[-2:]}\nUser: {input_text}\nAI:"
 
 
 
 
33
 
34
- # Tokenize the input and prepare it for inference
35
- inputs = tokenizer(prompt, return_tensors="pt").to(device) # Ensure using CPU
36
 
37
- # Set the max new tokens and temperature parameters for model generation
38
- output = model.generate(**inputs, max_new_tokens=max_tokens, temperature=temperature, use_cache=True)
 
 
 
 
39
 
40
- # Decode the model output and remove special tokens
41
- response = tokenizer.decode(output[0], skip_special_tokens=True)
42
 
43
- # Update history with the new conversation
44
  history.append(f"User: {input_text}")
45
  history.append(f"AI: {response}")
46
 
@@ -59,7 +63,7 @@ iface = gr.Interface(
59
  title="ISANG Chatbot",
60
  description="A chatbot powered by ISANG-v1.0-8B model. Chat with me!",
61
  theme="huggingface", # Purple theme
62
- live=True
63
  )
64
 
65
  # Launch the interface
 
8
  # Load model and tokenizer
9
  model_name = "hosseinhimself/ISANG-v1.0-8B" # Replace with your model name
10
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
 
 
11
  model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
12
 
13
  # Define the Alpaca-style prompt template
 
22
  ### Response:
23
  {}"""
24
 
25
+ # Function to generate responses
 
 
26
  def generate_response(input_text, max_tokens=1024, temperature=0.7, history=[]):
27
+ # Retain only the last two exchanges for context
28
+ if len(history) > 2:
29
+ history = history[-2:]
30
+
31
+ # Format the prompt
32
+ prompt = "\n".join(history + [f"User: {input_text}\nAI:"])
33
 
34
+ # Tokenize the input
35
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
36
 
37
+ # Generate model output
38
+ output = model.generate(
39
+ inputs.input_ids,
40
+ max_new_tokens=max_tokens,
41
+ temperature=temperature
42
+ )
43
 
44
+ # Decode the model output
45
+ response = tokenizer.decode(output[0], skip_special_tokens=True).strip()
46
 
47
+ # Update the history
48
  history.append(f"User: {input_text}")
49
  history.append(f"AI: {response}")
50
 
 
63
  title="ISANG Chatbot",
64
  description="A chatbot powered by ISANG-v1.0-8B model. Chat with me!",
65
  theme="huggingface", # Purple theme
66
+ live=False # Set to False since live updates aren't required
67
  )
68
 
69
  # Launch the interface