sifujohn commited on
Commit
88ace1c
1 Parent(s): 3b085a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+ client = InferenceClient("models/google/gemma-7b-it")
4
+
5
+
6
+ def format_prompt(message, history):
7
+ prompt = "<s>"
8
+ for user_prompt, bot_response in history:
9
+ prompt += f"[INST] {user_prompt} [/INST]"
10
+ prompt += f" {bot_response}</s> "
11
+ prompt += f"[INST] {message} [/INST]"
12
+ return prompt
13
+
14
+ def generate(
15
+ prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
16
+ ):
17
+ temperature = float(temperature)
18
+ if temperature < 1e-2:
19
+ temperature = 1e-2
20
+ top_p = float(top_p)
21
+
22
+ generate_kwargs = dict(
23
+ temperature=temperature,
24
+ max_new_tokens=max_new_tokens,
25
+ top_p=top_p,
26
+ repetition_penalty=repetition_penalty,
27
+ do_sample=True,
28
+ seed=42,
29
+ )
30
+
31
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
32
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
33
+ output = ""
34
+
35
+ for response in stream:
36
+ output += response.token.text
37
+ yield output
38
+ return output
39
+
40
+
41
+ additional_inputs=[
42
+ gr.Textbox(
43
+ label="System Prompt",
44
+ max_lines=1,
45
+ interactive=True,
46
+ ),
47
+ gr.Slider(
48
+ label="Temperature",
49
+ value=0.9,
50
+ minimum=0.0,
51
+ maximum=1.0,
52
+ step=0.05,
53
+ interactive=True,
54
+ info="Higher values produce more diverse outputs",
55
+ ),
56
+ gr.Slider(
57
+ label="Max new tokens",
58
+ value=256,
59
+ minimum=0,
60
+ maximum=1048,
61
+ step=64,
62
+ interactive=True,
63
+ info="The maximum numbers of new tokens",
64
+ ),
65
+ gr.Slider(
66
+ label="Top-p (nucleus sampling)",
67
+ value=0.90,
68
+ minimum=0.0,
69
+ maximum=1,
70
+ step=0.05,
71
+ interactive=True,
72
+ info="Higher values sample more low-probability tokens",
73
+ ),
74
+ gr.Slider(
75
+ label="Repetition penalty",
76
+ value=1.2,
77
+ minimum=1.0,
78
+ maximum=2.0,
79
+ step=0.05,
80
+ interactive=True,
81
+ info="Penalize repeated tokens",
82
+ )
83
+ ]
84
+
85
+ examples=[["I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?", None, None, None, None, None, ],
86
+ ["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
87
+ ["I'm trying to learn French. Can you provide some common phrases that would be useful for a beginner, along with their pronunciations?", None, None, None, None, None,],
88
+ ["I have chicken, rice, and bell peppers in my kitchen. Can you suggest an easy recipe I can make with these ingredients?", None, None, None, None, None,],
89
+ ["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
90
+ ["What are some unique features of Rust that make it stand out compared to other systems programming languages like C++?", None, None, None, None, None,],
91
+ ]
92
+
93
+ gr.ChatInterface(
94
+ fn=generate,
95
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
96
+ additional_inputs=additional_inputs,
97
+ title="Mixtral 46.7B",
98
+ examples=examples,
99
+ concurrency_limit=20,
100
+ ).launch(show_api=False)