Locutusque commited on
Commit
b87f04a
1 Parent(s): f74b566

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -31
app.py CHANGED
@@ -1,39 +1,42 @@
1
  import gradio as gr
2
- from transformers import pipeline, set_seed
3
  import torch
4
 
5
- # Function to generate responses using the entire conversation history
6
- def generate_response(messages, model_name, sampling_temperature, max_tokens, top_p):
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
- # Gradio chatbot interface with conversation history
23
- iface = gr.Interface(
24
- fn=generate_response,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  inputs=[
26
- gr.Chat("You", "Chatbot"),
27
- gr.Dropdown("Select Model", ["Locutusque/TinyMistral-248M-v2.5-Instruct", "Locutusque/Hercules-1.0-Mistral-7B", "Locutusque/UltraQwen-1_8B"]),
28
- gr.Slider("Sampling Temperature", 0.1, 2.0, 1.0, 0.1),
29
- gr.Slider("Max Tokens", 5, 200, 50, 5),
30
- gr.Slider("Top P", 0.1, 0.5, 0.75, 0.1)
 
31
  ],
32
- outputs=gr.Chat(role="Chatbot"),
33
- live=True,
34
- capture_session=True
 
35
  )
36
 
37
- # Launch Gradio chatbot interface
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)