deepapaikar commited on
Commit
7fdd3da
·
verified ·
1 Parent(s): b36f9f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -1,9 +1,12 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
  import spaces
4
 
5
- # Initialize the text generation pipeline outside the function for efficiency
6
- pipe = pipeline("text-generation", model="deepapaikar/LlamaKatz-3x8B")
 
 
7
 
8
  @spaces
9
  def generate_text(input_text):
@@ -15,11 +18,10 @@ def generate_text(input_text):
15
  Returns:
16
  str: The generated text.
17
  """
18
- messages = [
19
- {"role": "user", "content": input_text},
20
- ]
21
- output = pipe(messages)
22
- return output[0]['generated_text'] # Extract the generated text
23
 
24
  iface = gr.Interface(
25
  fn=generate_text,
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
  import spaces
5
 
6
+ # Load model and tokenizer only once, outside the function
7
+ model_name = "deepapaikar/LlamaKatz-3x8B"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map='auto')
10
 
11
  @spaces
12
  def generate_text(input_text):
 
18
  Returns:
19
  str: The generated text.
20
  """
21
+ inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
22
+ outputs = model.generate(**inputs)
23
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
+ return generated_text
 
25
 
26
  iface = gr.Interface(
27
  fn=generate_text,