crystal99 commited on
Commit
05eaf03
·
verified ·
1 Parent(s): 5f9b31b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
@@ -18,3 +19,32 @@ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Tex
18
 
19
  # Launch the Gradio interface
20
  iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
  import gradio as gr
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
 
19
 
20
  # Launch the Gradio interface
21
  iface.launch()
22
+ """
23
+
24
+ import gradio as gr
25
+ from transformers import AutoModelForCausalLM, AutoTokenizer
26
+ import torch
27
+
28
+ # Load your fine-tuned model and tokenizer
29
+ model_name = "crystal99/my-fine-tuned-model"
30
+ model = AutoModelForCausalLM.from_pretrained(model_name)
31
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
32
+
33
+ # Move model to GPU if available and enable fp16 for faster inference
34
+ device = "cuda" if torch.cuda.is_available() else "cpu"
35
+ model.to(device)
36
+
37
+ # Define the text generation function
38
+ def generate_text(prompt):
39
+ # Prevent gradient calculation to speed up inference
40
+ with torch.no_grad():
41
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
42
+ outputs = model.generate(inputs['input_ids'], max_length=100, num_return_sequences=1, do_sample=False)
43
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
+ return generated_text
45
+
46
+ # Set up the Gradio interface
47
+ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Text Generator using Fine-Tuned Model")
48
+
49
+ # Launch the Gradio interface
50
+ iface.launch()