Spaces:
Sleeping
Sleeping
Locutusque
commited on
Commit
•
b87f04a
1
Parent(s):
f74b566
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline,
|
3 |
import torch
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
generator = pipeline('text-generation', model=model_name, torch_dtype=torch.float16)
|
8 |
-
set_seed(42) # You can set a different seed for reproducibility
|
9 |
-
|
10 |
-
# Combine entire conversation history
|
11 |
-
conversation = ""
|
12 |
-
for message in messages:
|
13 |
-
role = message['role']
|
14 |
-
content = message['content']
|
15 |
-
conversation += f"<|im_start|>{role}\n{content}<|im_end|>\n"
|
16 |
-
|
17 |
-
# Generate response
|
18 |
-
response = generator(conversation, max_length=2048, temperature=sampling_temperature, max_tokens=max_tokens, top_p=top_p, repetition_penalty=1.1, top_k=12)
|
19 |
-
|
20 |
-
return [{'content': response[0]['generated_text'], 'role': 'assistant'}]
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
inputs=[
|
26 |
-
gr.
|
27 |
-
gr.
|
28 |
-
gr.Slider(
|
29 |
-
gr.Slider(
|
30 |
-
gr.Slider(
|
|
|
31 |
],
|
32 |
-
outputs=gr.
|
33 |
-
|
34 |
-
|
|
|
35 |
)
|
36 |
|
37 |
-
|
38 |
-
iface.launch()
|
39 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
+
def load_model(model_name):
|
6 |
+
return pipeline("text-generation", model=model_name, device="cuda", torch_dtype=torch.float16)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def generate(
|
9 |
+
model_name,
|
10 |
+
user_input,
|
11 |
+
temperature=0.4,
|
12 |
+
top_p=0.95,
|
13 |
+
top_k=50,
|
14 |
+
max_new_tokens=256,
|
15 |
+
):
|
16 |
+
pipe = load_model(model_name)
|
17 |
+
|
18 |
+
# Set tokenize correctly. Otherwise ticking the box breaks it.
|
19 |
+
prompt = f"<|im_start|>user\n{user_input}<|im_end|>\n"
|
20 |
+
outputs = pipe(prompt, max_new_tokens=max_new_tokens, do_sample=True,
|
21 |
+
temperature=temperature, top_k=top_k, top_p=top_p, repetition_penalty=1.10)
|
22 |
+
return outputs[0]["generated_text"]
|
23 |
+
|
24 |
+
model_choices = ["Locutusque/UltraQwen-7B", "Locutusque/UltraQwen-1_8B", "Locutusque/TinyMistral-248M-v2.5-Instruct", "M4-ai/TinyMistral-6x248M-Instruct"]
|
25 |
+
# What at the best options?
|
26 |
+
g = gr.Interface(
|
27 |
+
fn=generate,
|
28 |
inputs=[
|
29 |
+
gr.components.Dropdown(choices=model_choices, label="Model", value=model_choices[0], interactive=True),
|
30 |
+
gr.components.Textbox(lines=2, label="Prompt", value="How many planets are in our solar system?"),
|
31 |
+
gr.components.Slider(minimum=0, maximum=1, value=0.4, label="Temperature"),
|
32 |
+
gr.components.Slider(minimum=0, maximum=1, value=0.95, label="Top p"),
|
33 |
+
gr.components.Slider(minimum=0, maximum=100, step=1, value=50, label="Top k"),
|
34 |
+
gr.components.Slider(minimum=1, maximum=1024, step=1, value=256, label="Max tokens"),
|
35 |
],
|
36 |
+
outputs=[gr.Textbox(lines=10, label="Output")],
|
37 |
+
title="Hugging Face Transformers Model",
|
38 |
+
description="A simple interface for generating text with a Hugging Face Transformers model.",
|
39 |
+
concurrency_limit=1
|
40 |
)
|
41 |
|
42 |
+
g.launch(max_threads=2)
|
|
|
|