Satyam-Singh commited on
Commit
ffb41cc
1 Parent(s): bc19271

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -0
app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+
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
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
14
+
15
+ def generate(
16
+ prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
17
+ ):
18
+ temperature = float(temperature)
19
+ if temperature < 1e-2:
20
+ temperature = 1e-2
21
+ top_p = float(top_p)
22
+
23
+ generate_kwargs = dict(
24
+ temperature=temperature,
25
+ max_new_tokens=max_new_tokens,
26
+ top_p=top_p,
27
+ repetition_penalty=repetition_penalty,
28
+ do_sample=True,
29
+ seed=42,
30
+ )
31
+
32
+ formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
33
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
34
+ output = ""
35
+
36
+ for response in stream:
37
+ output += response.token.text
38
+ yield output
39
+ return output
40
+
41
+
42
+ additional_inputs=[
43
+ gr.Textbox(
44
+ label="System Prompt",
45
+ max_lines=1,
46
+ interactive=True,
47
+ ),
48
+ gr.Slider(
49
+ label="Temperature",
50
+ value=0.9,
51
+ minimum=0.0,
52
+ maximum=1.0,
53
+ step=0.05,
54
+ interactive=True,
55
+ info="Higher values produce more diverse outputs",
56
+ ),
57
+ gr.Slider(
58
+ label="Max new tokens",
59
+ value=4096,
60
+ minimum=0,
61
+ maximum=8192,
62
+ step=64,
63
+ interactive=True,
64
+ info="The maximum numbers of new tokens",
65
+ ),
66
+ gr.Slider(
67
+ label="Top-p (nucleus sampling)",
68
+ value=0.90,
69
+ minimum=0.0,
70
+ maximum=1,
71
+ step=0.05,
72
+ interactive=True,
73
+ info="Higher values sample more low-probability tokens",
74
+ ),
75
+ gr.Slider(
76
+ label="Repetition penalty",
77
+ value=1.2,
78
+ minimum=1.0,
79
+ maximum=2.0,
80
+ step=0.05,
81
+ interactive=True,
82
+ info="Penalize repeated tokens",
83
+ )
84
+ ]
85
+
86
+ 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, ],
87
+ ["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
88
+ ["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,],
89
+ ["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,],
90
+ ["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
91
+ ["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,],
92
+ ]
93
+
94
+ gr.ChatInterface(
95
+ fn=generate,
96
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
97
+ additional_inputs=additional_inputs,
98
+ title="LLaVa 56B Large Language Virtual Assiatant",
99
+ examples=examples,
100
+ concurrency_limit=20,
101
+ ).launch(share=True,show_api=True)