Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_name = "DAMO-NLP-SG/VideoLLaMA3-7B"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def generate_response(prompt, max_tokens=200, temperature=0.7):
|
| 12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
output = model.generate(
|
| 15 |
+
**inputs,
|
| 16 |
+
max_new_tokens=max_tokens,
|
| 17 |
+
temperature=temperature,
|
| 18 |
+
do_sample=True,
|
| 19 |
+
top_p=0.9,
|
| 20 |
+
eos_token_id=tokenizer.eos_token_id
|
| 21 |
+
)
|
| 22 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
return response[len(prompt):].strip() # Return only the generated part
|
| 24 |
+
|
| 25 |
+
# Gradio UI
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=generate_response,
|
| 28 |
+
inputs=[
|
| 29 |
+
gr.Textbox(label="Prompt", lines=5, placeholder="Enter your prompt here..."),
|
| 30 |
+
gr.Slider(minimum=50, maximum=1000, value=200, label="Max Tokens"),
|
| 31 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature"),
|
| 32 |
+
],
|
| 33 |
+
outputs=gr.Textbox(label="Response"),
|
| 34 |
+
title="VideoLLaMA3-7B Text Generation",
|
| 35 |
+
description="Generate text using DAMO-NLP-SG/VideoLLaMA3-7B"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
iface.launch()
|