MuntasirHossain
commited on
Commit
•
ef831a4
1
Parent(s):
f4b9247
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
inference_client = InferenceClient("google/gemma-7b-it")
|
5 |
+
|
6 |
+
# format prompt as per the chat template on the official model page: https://huggingface.co/google/gemma-7b-it
|
7 |
+
def format_prompt(input_text, history):
|
8 |
+
prompt = ""
|
9 |
+
if history:
|
10 |
+
for previous_prompt, response in history:
|
11 |
+
prompt += f"""<start_of_turn>user
|
12 |
+
{previous_prompt}<end_of_turn>
|
13 |
+
<start_of_turn>model
|
14 |
+
{response}<end_of_turn>"""
|
15 |
+
prompt += f"""<start_of_turn>user
|
16 |
+
{input_text}<end_of_turn>
|
17 |
+
<start_of_turn>model"""
|
18 |
+
return prompt
|
19 |
+
|
20 |
+
def generate(prompt, history):
|
21 |
+
if not history:
|
22 |
+
history = []
|
23 |
+
|
24 |
+
kwargs = dict(
|
25 |
+
temperature=1.0,
|
26 |
+
max_new_tokens=512,
|
27 |
+
top_p=0.9,
|
28 |
+
repetition_penalty=1,
|
29 |
+
do_sample=True,
|
30 |
+
)
|
31 |
+
|
32 |
+
formatted_prompt = format_prompt(prompt, history)
|
33 |
+
|
34 |
+
response = inference_client.text_generation(formatted_prompt, **kwargs, stream=True, details=True, return_full_text=True)
|
35 |
+
output = ""
|
36 |
+
|
37 |
+
for chunk in response:
|
38 |
+
output += chunk.token.text
|
39 |
+
yield output
|
40 |
+
return output
|
41 |
+
|
42 |
+
|
43 |
+
chatbot = gr.Chatbot(height=500)
|
44 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
45 |
+
gr.HTML("<center><h1>Google Gemma 7B IT</h1><center>")
|
46 |
+
gr.ChatInterface(
|
47 |
+
generate,
|
48 |
+
chatbot=chatbot,
|
49 |
+
retry_btn=None,
|
50 |
+
undo_btn=None,
|
51 |
+
clear_btn="Clear",
|
52 |
+
description="This chatbot is using a Hugging Face Inference Client for the google/gemma-7b-it model.",
|
53 |
+
examples=[["Explain artificial intelligence in a few lines."]]
|
54 |
+
)
|
55 |
+
demo.queue().launch()
|