Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3-70B-Instruct")
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
"meta-llama/Meta-Llama-3-70B-Instruct",
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
device_map="auto"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Inference function
|
| 14 |
+
def generate_response(prompt, max_tokens=256, temperature=0.7):
|
| 15 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 16 |
+
outputs = model.generate(
|
| 17 |
+
**inputs,
|
| 18 |
+
max_new_tokens=max_tokens,
|
| 19 |
+
temperature=temperature,
|
| 20 |
+
do_sample=True,
|
| 21 |
+
top_p=0.95,
|
| 22 |
+
eos_token_id=tokenizer.eos_token_id
|
| 23 |
+
)
|
| 24 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
gr.Interface(
|
| 28 |
+
fn=generate_response,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.Textbox(lines=4, label="Prompt"),
|
| 31 |
+
gr.Slider(50, 1024, step=10, value=256, label="Max Tokens"),
|
| 32 |
+
gr.Slider(0.1, 1.5, step=0.1, value=0.7, label="Temperature")
|
| 33 |
+
],
|
| 34 |
+
outputs=gr.Textbox(label="Generated Response"),
|
| 35 |
+
title="Meta LLaMA 3 70B Instruct",
|
| 36 |
+
description="Gradio demo for Meta-Llama-3-70B-Instruct"
|
| 37 |
+
).launch()
|