File size: 5,934 Bytes
34c466e
7b9d97a
34c466e
 
 
 
 
 
7b9d97a
 
 
 
 
 
 
 
34c466e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bf32ee
 
 
 
 
 
 
34c466e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85ce618
34c466e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a11631
 
4687fd0
34c466e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e460e
34c466e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85ce618
34c466e
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Import necessary libraries
import os
from threading import Thread
import argparse
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, TextIteratorStreamer, AutoModelForCausalLM
from peft import PeftConfig, PeftModel
from utils import get_device  # Assuming this function exists
from huggingface_hub import login

# Authenticate using Hugging Face API token from environment variable
hf_api_token = os.getenv("HF_API_TOKEN")
if hf_api_token is None:
    raise ValueError("Hugging Face API token not found in environment variables. Please set it as a secret in Hugging Face Spaces.")
login(token=hf_api_token)

# Create the parser
parser = argparse.ArgumentParser(description='Check model usage.')

# Add the arguments
parser.add_argument('--baseonly', action='store_true', 
                    help='A boolean switch to indicate base only mode')

# Execute the parse_args() method
args = parser.parse_args()

# Define model and adapter names, data type, and quantization type
model_name = "microsoft/Phi-3-mini-4k-instruct"
adapters_name = "zurd46/eliAI"
torch_dtype = torch.bfloat16  # Set the appropriate torch data type

# Display device and CPU thread information
device = get_device()
print(f"Number of GPUs available: {torch.cuda.device_count()}")
print(f"Running on device: {device}")
print(f"CPU threads: {torch.get_num_threads()}")

# Check if CUDA is available and set the device accordingly
if not torch.cuda.is_available():
    raise RuntimeError("CUDA is not available. Ensure that a GPU is available and properly configured.")

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Load base model
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch_dtype)
model.resize_token_embeddings(len(tokenizer))

# Load adapter if available and not baseonly
usingAdapter = False
if not args.baseonly:
    usingAdapter = True
    model = PeftModel.from_pretrained(model, adapters_name)

model.to(device)

print(f"Model {model_name} loaded successfully on {device}")

# Function to run the text generation process
def run_generation(user_text, top_p, temperature, top_k, max_new_tokens):
    template = "<|context|><|user|>\n{}<|end|>\n<|assistant|>"
    model_inputs = tokenizer(template.format(user_text) if usingAdapter else user_text, return_tensors="pt")
    model_inputs = model_inputs.to(device)

    # Generate text in a separate thread
    streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
    generate_kwargs = dict(
        input_ids=model_inputs['input_ids'],
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        top_p=top_p,
        temperature=float(temperature),
        top_k=top_k,
        pad_token_id=tokenizer.pad_token_id,
        eos_token_id=tokenizer.eos_token_id
    )
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    # Retrieve and yield the generated text
    model_output = ""
    for new_text in streamer:
        model_output += new_text
    return model_output

# Gradio UI setup
with gr.Blocks(css="""
    .form.svelte-sfqy0y {
      background: var(--block-background-fill);
      padding: 20px;
    }
    body {
        font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
        color: #e0e0e0;
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    .gradio-container {
        max-width: 900px;
        margin: auto;
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 0 10px rgba(0,0,0,0.5);
    }
    .gr-button {
        color: white;
        border: none;
        border-radius: 4px;
        padding: 10px 24px;
        cursor: pointer;
    }
    .gr-button:hover {
        background-color: #3700b3;
    }
    .gr-slider input[type=range] {
        -webkit-appearance: none;
        width: 100%;
        height: 8px;
        border-radius: 5px;
        outline: none;
        opacity: 0.9;
        -webkit-transition: .2s;
        transition: opacity .2s;
    }
    .gr-slider input[type=range]:hover {
        opacity: 1;
    }
    .gr-textbox {
        color: white;
        border: none;
        border-radius: 4px;
        padding: 10px;
    }
    .chatbox {
        max-height: 400px;
        overflow-y: auto;
        margin-bottom: 20px;
    }
""") as demo:
    gr.Markdown(
        """
        <div style="text-align: center; padding: 20px;">
            <h1>🌙 eliAI Text Generation Interface</h1>
            <h3>Model: Phi-3-mini-4k-instruct</h3>
            <h4>Developed by Daniel Zurmühle</h4>
        </div>
        """)
    
    with gr.Row():
        with gr.Column(scale=3):
            user_text = gr.Textbox(placeholder="Enter your question here", label="User Input", lines=3, elem_classes="gr-textbox")
            button_submit = gr.Button(value="Submit", elem_classes="gr-button")

            max_new_tokens = gr.Slider(minimum=1, maximum=1000, value=1000, step=1, label="Max New Tokens")
            top_p = gr.Slider(minimum=0.05, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)")
            top_k = gr.Slider(minimum=1, maximum=50, value=50, step=1, label="Top-k")
            temperature = gr.Slider(minimum=0.1, maximum=5.0, value=0.8, step=0.1, label="Temperature")
        
        with gr.Column(scale=7):
            model_output = gr.Chatbot(label="Chatbot Output", height=566)

    def handle_submit(text, top_p, temperature, top_k, max_new_tokens):
        response = run_generation(text, top_p, temperature, top_k, max_new_tokens)
        return [(text, response)]

    button_submit.click(handle_submit, [user_text, top_p, temperature, top_k, max_new_tokens], model_output)
    user_text.submit(handle_submit, [user_text, top_p, temperature, top_k, max_new_tokens], model_output)

    demo.queue(max_size=32).launch(server_name="0.0.0.0", server_port=7860)