init
Browse files- app.py +46 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
4 |
+
import gradio as gr
|
5 |
+
import spaces
|
6 |
+
|
7 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
8 |
+
if not huggingface_token:
|
9 |
+
raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
|
10 |
+
|
11 |
+
model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
12 |
+
device = "cuda"
|
13 |
+
dtype = torch.bfloat16
|
14 |
+
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=huggingface_token)
|
16 |
+
|
17 |
+
|
18 |
+
@spaces.GPU
|
19 |
+
def generate_text(prompt, system_message="You are a helpful assistant."):
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
21 |
+
model_id, torch_dtype=dtype, device_map=device, token=huggingface_token
|
22 |
+
)
|
23 |
+
text_generator = pipeline("text-generation", model=model, tokenizer=tokenizer, torch_dtype=dtype, device_map=device)
|
24 |
+
|
25 |
+
messages = [
|
26 |
+
{"role": "system", "content": system_message},
|
27 |
+
{"role": "user", "content": prompt},
|
28 |
+
]
|
29 |
+
|
30 |
+
result = text_generator(messages, max_new_tokens=256, do_sample=True, temperature=0.7)
|
31 |
+
return result[0]["generated_text"]
|
32 |
+
|
33 |
+
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=generate_text,
|
36 |
+
inputs=[
|
37 |
+
gr.Textbox(lines=3, label="Input Prompt"),
|
38 |
+
gr.Textbox(lines=2, label="System Message", value="You are a helpful assistant."),
|
39 |
+
],
|
40 |
+
outputs=gr.Textbox(label="Generated Text"),
|
41 |
+
title="Llama 3.1 8B Instruct Text Generation",
|
42 |
+
description="Enter a prompt and optional system message to generate text using the Llama 3.1 8B Instruct model.",
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
torch
|
3 |
+
spaces
|
4 |
+
bitsandbytes
|
5 |
+
accelerate
|
6 |
+
transformers
|