Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,52 +15,26 @@ app = gr.load(
|
|
| 15 |
]
|
| 16 |
).launch()
|
| 17 |
"""
|
| 18 |
-
"""
|
| 19 |
-
# Pipeline
|
| 20 |
-
|
| 21 |
-
import gradio as gr
|
| 22 |
-
from transformers import pipeline
|
| 23 |
-
|
| 24 |
-
pipe = pipeline(model = "google/gemma-2-2b-it")
|
| 25 |
|
| 26 |
-
|
| 27 |
-
output = pipe(
|
| 28 |
-
input,
|
| 29 |
-
max_new_tokens = 2048
|
| 30 |
-
)
|
| 31 |
-
return output[0]["generated_text"]#[len(input):]
|
| 32 |
-
|
| 33 |
-
app = gr.Interface(
|
| 34 |
-
fn = fn,
|
| 35 |
-
inputs = [gr.Textbox(label = "Input")],
|
| 36 |
-
outputs = [gr.Textbox(label = "Output")],
|
| 37 |
-
title = "Google Gemma",
|
| 38 |
-
description = "Pipeline",
|
| 39 |
-
examples = [
|
| 40 |
-
["Hello, World."]
|
| 41 |
-
]
|
| 42 |
-
).launch()
|
| 43 |
-
"""
|
| 44 |
|
| 45 |
import gradio as gr
|
| 46 |
from huggingface_hub import InferenceClient
|
| 47 |
-
import os
|
| 48 |
|
| 49 |
#hf_token = os.getenv("HF_TOKEN")
|
| 50 |
|
| 51 |
client = InferenceClient("google/gemma-2-2b-it")
|
| 52 |
|
| 53 |
-
def
|
| 54 |
message,
|
| 55 |
history: list[tuple[str, str]],
|
| 56 |
#system_message,
|
| 57 |
-
##user_message,
|
| 58 |
max_tokens,
|
| 59 |
temperature,
|
| 60 |
top_p,
|
| 61 |
):
|
| 62 |
#messages = [{"role": "system", "content": system_message}]
|
| 63 |
-
##messages = [{"role": "user", "content": user_message}]
|
| 64 |
messages = []
|
| 65 |
|
| 66 |
for val in history:
|
|
@@ -75,61 +49,55 @@ def respond(
|
|
| 75 |
|
| 76 |
for message in client.chat_completion(
|
| 77 |
messages,
|
| 78 |
-
max_tokens=max_tokens,
|
| 79 |
-
temperature=temperature,
|
| 80 |
-
top_p=top_p,
|
| 81 |
-
stream=True,
|
| 82 |
):
|
| 83 |
token = message.choices[0].delta.content
|
| 84 |
|
| 85 |
response += token
|
| 86 |
yield response
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
additional_inputs=[
|
| 91 |
#gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 92 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 93 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 94 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 95 |
],
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|
| 100 |
-
|
| 101 |
"""
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
for user_prompt, bot_response in history:
|
| 108 |
-
messages.append({"role": "user", "content": user_prompt})
|
| 109 |
-
messages.append({"role": "bot", "content": bot_response})
|
| 110 |
-
|
| 111 |
-
messages.append({"role": "user", "content": prompt})
|
| 112 |
-
|
| 113 |
-
stream = client.chat.completions.create(
|
| 114 |
-
model = "google/gemma-2-2b-it",
|
| 115 |
-
messages = messages,
|
| 116 |
-
#temperature = 0.5,
|
| 117 |
-
#max_tokens = 2048,
|
| 118 |
-
#top_p = 0.7,
|
| 119 |
-
stream = True
|
| 120 |
-
)
|
| 121 |
|
| 122 |
-
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
app = gr.Interface(
|
| 128 |
-
fn = fn,
|
| 129 |
inputs = [gr.Textbox(label = "Input")],
|
| 130 |
outputs = [gr.Textbox(label = "Output")],
|
| 131 |
title = "Google Gemma",
|
| 132 |
-
description = "
|
| 133 |
examples = [
|
| 134 |
["Hello, World."]
|
| 135 |
]
|
|
|
|
| 15 |
]
|
| 16 |
).launch()
|
| 17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Inference Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
import gradio as gr
|
| 22 |
from huggingface_hub import InferenceClient
|
| 23 |
+
#import os
|
| 24 |
|
| 25 |
#hf_token = os.getenv("HF_TOKEN")
|
| 26 |
|
| 27 |
client = InferenceClient("google/gemma-2-2b-it")
|
| 28 |
|
| 29 |
+
def fn_chat(
|
| 30 |
message,
|
| 31 |
history: list[tuple[str, str]],
|
| 32 |
#system_message,
|
|
|
|
| 33 |
max_tokens,
|
| 34 |
temperature,
|
| 35 |
top_p,
|
| 36 |
):
|
| 37 |
#messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 38 |
messages = []
|
| 39 |
|
| 40 |
for val in history:
|
|
|
|
| 49 |
|
| 50 |
for message in client.chat_completion(
|
| 51 |
messages,
|
| 52 |
+
max_tokens = max_tokens,
|
| 53 |
+
temperature = temperature,
|
| 54 |
+
top_p = top_p,
|
| 55 |
+
stream = True,
|
| 56 |
):
|
| 57 |
token = message.choices[0].delta.content
|
| 58 |
|
| 59 |
response += token
|
| 60 |
yield response
|
| 61 |
|
| 62 |
+
app = gr.ChatInterface(
|
| 63 |
+
fn = fn_chat,
|
| 64 |
+
additional_inputs = [
|
| 65 |
#gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 66 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 67 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 68 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 69 |
],
|
| 70 |
+
title = "Google Gemma",
|
| 71 |
+
description = "Chatbot",
|
| 72 |
+
examples = [
|
| 73 |
+
["Hello, World."]
|
| 74 |
+
]
|
| 75 |
+
).launch
|
| 76 |
+
"""
|
| 77 |
if __name__ == "__main__":
|
| 78 |
demo.launch()
|
|
|
|
| 79 |
"""
|
| 80 |
+
"""
|
| 81 |
+
# Pipeline
|
| 82 |
|
| 83 |
+
import gradio as gr
|
| 84 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
pipe = pipeline(model = "google/gemma-2-2b-it")
|
| 87 |
|
| 88 |
+
def fn(input):
|
| 89 |
+
output = pipe(
|
| 90 |
+
input,
|
| 91 |
+
max_new_tokens = 2048
|
| 92 |
+
)
|
| 93 |
+
return output[0]["generated_text"]#[len(input):]
|
| 94 |
|
| 95 |
app = gr.Interface(
|
| 96 |
+
fn = fn,
|
| 97 |
inputs = [gr.Textbox(label = "Input")],
|
| 98 |
outputs = [gr.Textbox(label = "Output")],
|
| 99 |
title = "Google Gemma",
|
| 100 |
+
description = "Pipeline",
|
| 101 |
examples = [
|
| 102 |
["Hello, World."]
|
| 103 |
]
|