Spaces:
Sleeping
Sleeping
asd
Browse files
app.py
CHANGED
@@ -1,7 +1,35 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import gradio as gr
|
4 |
+
import spaces
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "NoaiGPT/merged-llama3-8b-instruct-1720894657"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
+
# Move model to GPU if available
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model.to(device)
|
13 |
|
14 |
+
# Define the prediction function
|
15 |
+
@spaces.GPU
|
16 |
+
def generate_text(prompt):
|
17 |
+
# Tokenize the input and move to GPU if available
|
18 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
19 |
+
# Generate text using the model
|
20 |
+
outputs = model.generate(inputs.input_ids, max_length=200, num_return_sequences=1)
|
21 |
+
# Decode the generated text
|
22 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
+
return generated_text
|
24 |
+
|
25 |
+
# Define the Gradio interface
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=generate_text,
|
28 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
29 |
+
outputs="text",
|
30 |
+
title="LLaMA 3 Text Generation",
|
31 |
+
description="Generate text using the LLaMA 3 model fine-tuned for instruction-following tasks."
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the interface
|
35 |
+
interface.launch()
|