hosseinhimself commited on
Commit
4b4deff
·
verified ·
1 Parent(s): 1f688a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -1,24 +1,40 @@
1
  import gradio as gr
2
  from unsloth import FastLanguageModel
3
  from transformers import AutoTokenizer, TextStreamer
 
4
 
5
- # Load the tokenizer
6
- model_name = "hosseinhimself/ISANG-v1.0-8B"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
  # Initialize the model for faster inference using Unsloth
10
  model = FastLanguageModel.from_pretrained(model_name)
11
  FastLanguageModel.for_inference(model) # Enable faster inference
12
 
13
- # Set hyperparameters for inference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def generate_response(input_text, max_tokens=1024, temperature=0.7, history=[]):
15
- # Prepare the inputs for the model
16
  prompt = f"Chat History: {history[-2:]}\nUser: {input_text}\nAI:"
17
 
 
18
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
19
 
20
  # Set the max new tokens and temperature parameters for model generation
21
- output = model.generate(**inputs, max_new_tokens=max_tokens, temperature=temperature)
22
 
23
  # Decode the model output and remove special tokens
24
  response = tokenizer.decode(output[0], skip_special_tokens=True)
@@ -36,12 +52,12 @@ iface = gr.Interface(
36
  gr.Textbox(label="Your Message", placeholder="Type your message here..."),
37
  gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max Tokens"),
38
  gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
39
- gr.State(value=[])
40
  ],
41
  outputs=[gr.Textbox(label="AI Response"), gr.State()],
42
  title="ISANG Chatbot",
43
  description="A chatbot powered by ISANG-v1.0-8B model. Chat with me!",
44
- theme="huggingface",
45
  live=True
46
  )
47
 
 
1
  import gradio as gr
2
  from unsloth import FastLanguageModel
3
  from transformers import AutoTokenizer, TextStreamer
4
+ import torch
5
 
6
+ # Load model and tokenizer
7
+ model_name = "unsloth/Meta-Llama-3.1-8B" # Replace with your model if needed
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
  # Initialize the model for faster inference using Unsloth
11
  model = FastLanguageModel.from_pretrained(model_name)
12
  FastLanguageModel.for_inference(model) # Enable faster inference
13
 
14
+ # Define the Alpaca-style prompt template
15
+ alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
16
+
17
+ ### Instruction:
18
+ You are ISANG, a multilingual large language model made by ISANG AI. You only respond in Persian, Korean, or English. If a user uses one of these languages, reply in the same language.
19
+
20
+ ### Input:
21
+ {}
22
+
23
+ ### Response:
24
+ {}"""
25
+
26
+ EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
27
+
28
+ # Define a function to generate responses
29
  def generate_response(input_text, max_tokens=1024, temperature=0.7, history=[]):
30
+ # Prepare the inputs for the model with the history
31
  prompt = f"Chat History: {history[-2:]}\nUser: {input_text}\nAI:"
32
 
33
+ # Tokenize the input and prepare it for inference
34
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
35
 
36
  # Set the max new tokens and temperature parameters for model generation
37
+ output = model.generate(**inputs, max_new_tokens=max_tokens, temperature=temperature, use_cache=True)
38
 
39
  # Decode the model output and remove special tokens
40
  response = tokenizer.decode(output[0], skip_special_tokens=True)
 
52
  gr.Textbox(label="Your Message", placeholder="Type your message here..."),
53
  gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max Tokens"),
54
  gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
55
+ gr.State(value=[]) # To maintain conversation history
56
  ],
57
  outputs=[gr.Textbox(label="AI Response"), gr.State()],
58
  title="ISANG Chatbot",
59
  description="A chatbot powered by ISANG-v1.0-8B model. Chat with me!",
60
+ theme="huggingface", # Purple theme
61
  live=True
62
  )
63