Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
import spaces
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Model and Tokenizer
|
| 7 |
+
model_name = "meta-llama/Llama-2-7b-hf" # Change to 13B or 70B if needed
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
model_name,
|
| 13 |
+
torch_dtype=torch.float16, # Enable FP16
|
| 14 |
+
device_map="auto" # Automatically place model on GPU
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Inference Function
|
| 18 |
+
@spaces.GPU
|
| 19 |
+
def generate_text(prompt):
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 21 |
+
output = model.generate(**inputs, max_new_tokens=100)
|
| 22 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
# Example Usage
|
| 25 |
+
|
| 26 |
+
@spaces.GPU
|
| 27 |
+
def chat_with_llama(prompt):
|
| 28 |
+
return generate_text(prompt)
|
| 29 |
+
|
| 30 |
+
gr.Interface(fn=chat_with_llama, inputs="text", outputs="text", title="LLaMA 2 Chatbot").launch()
|
| 31 |
+
|