ryanpdwyer
commited on
Commit
·
c717637
1
Parent(s):
1f5c613
Updated text gen interface
Browse files- app.py +32 -3
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,6 +1,35 @@
|
|
1 |
-
import gradio as gr
|
2 |
import spaces
|
|
|
|
|
|
|
|
|
3 |
|
4 |
@spaces.GPU
|
5 |
-
def
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
model_name = "models/unsloth/Meta-Llama-3.1-8B-bnb-4bit"
|
6 |
|
7 |
@spaces.GPU
|
8 |
+
def load_model():
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
return model, tokenizer
|
12 |
+
|
13 |
+
@spaces.GPU
|
14 |
+
def generate_text(prompt, model, tokenizer):
|
15 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
16 |
+
outputs = model.generate(**inputs, max_length=100)
|
17 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
|
19 |
+
def gradio_interface():
|
20 |
+
model, tokenizer = load_model()
|
21 |
+
|
22 |
+
def wrapped_generate(prompt):
|
23 |
+
return generate_text(prompt, model, tokenizer)
|
24 |
+
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=wrapped_generate,
|
27 |
+
inputs="text",
|
28 |
+
outputs="text",
|
29 |
+
title="Meta-Llama 3.1 8B Text Generation"
|
30 |
+
)
|
31 |
+
return iface
|
32 |
+
|
33 |
+
if __name__ == "__main__":
|
34 |
+
demo = gradio_interface()
|
35 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
huggingface_hub==0.22.2
|
2 |
-
spaces
|
|
|
|
1 |
huggingface_hub==0.22.2
|
2 |
+
spaces
|
3 |
+
transformers
|