Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from IPython.display import Markdown, display, update_display
|
4 |
+
from openai import OpenAI
|
5 |
+
from google.colab import drive
|
6 |
+
from huggingface_hub import login
|
7 |
+
from google.colab import userdata
|
8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer, BitsAndBytesConfig
|
9 |
+
import torch
|
10 |
+
|
11 |
+
|
12 |
+
LLAMA = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
13 |
+
|
14 |
+
hf_token = userdata.get('HF_TOKEN')
|
15 |
+
login(hf_token, add_to_git_credential=True)
|
16 |
+
|
17 |
+
system_message = "You are an assistant that produces datasets based on description provided."
|
18 |
+
user_input = "Film critics of 1900s"
|
19 |
+
user_prompt = f"Below is the description for which you need to generate dataset.\n{user_input}"
|
20 |
+
|
21 |
+
messages = [
|
22 |
+
{"role": "system", "content": system_message},
|
23 |
+
{"role": "user", "content": user_prompt}
|
24 |
+
]
|
25 |
+
|
26 |
+
quant_config = BitsAndBytesConfig(
|
27 |
+
load_in_4bit=True,
|
28 |
+
bnb_4bit_use_double_quant=True,
|
29 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
30 |
+
bnb_4bit_quant_type="nf4"
|
31 |
+
)
|
32 |
+
|
33 |
+
tokenizer = None
|
34 |
+
model = None
|
35 |
+
streamer = None
|
36 |
+
|
37 |
+
|
38 |
+
def run_llama(python):
|
39 |
+
if tokenizer is None:
|
40 |
+
tokenizer = AutoTokenizer.from_pretrained(LLAMA)
|
41 |
+
tokenizer.pad_token = tokenizer.eos_token
|
42 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to("cuda")
|
43 |
+
# streamer = TextStreamer(tokenizer)
|
44 |
+
if model is None:
|
45 |
+
model = AutoModelForCausalLM.from_pretrained(LLAMA, device_map="auto", quantization_config=quant_config)
|
46 |
+
outputs = model.generate(inputs, max_new_tokens=2000, streamer=streamer)
|
47 |
+
|
48 |
+
response = tokenizer.decode(outputs[0])
|
49 |
+
return response
|
50 |
+
|
51 |
+
|
52 |
+
# Gradio Interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=run_llama,
|
55 |
+
inputs=gr.Textbox(label="Enter dataset description"),
|
56 |
+
outputs=gr.Markdown(label="Generated Dataset"),
|
57 |
+
title="Dataset Generator",
|
58 |
+
description="Describe the dataset you want to generate."
|
59 |
+
)
|
60 |
+
|
61 |
+
iface.launch(share=True, debug=True)
|