Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,59 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
messages,
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
gr.Slider(
|
| 51 |
-
gr.Slider(
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import transformers
|
| 3 |
+
import torch
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
model_id = "JerniganLab/qa-only"
|
| 10 |
+
base_model = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 11 |
|
| 12 |
+
llama_model = transformers.AutoModelForCausalLM.from_pretrained(base_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
pipeline = transformers.pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model=llama_model,
|
| 18 |
+
tokenizer=base_model,
|
| 19 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
| 20 |
+
device="cuda",
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
pipeline.model = PeftModel.from_pretrained(llama_model, model_id)
|
| 24 |
|
| 25 |
+
def chat_function(message, history, system_prompt, max_new_tokens, temperature):
|
| 26 |
+
messages = [{"role":"system","content":system_prompt},
|
| 27 |
+
{"role":"user", "content":message}]
|
| 28 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
| 29 |
messages,
|
| 30 |
+
tokenize=False,
|
| 31 |
+
add_generation_prompt=True,)
|
| 32 |
+
terminators = [
|
| 33 |
+
pipeline.tokenizer.eos_token_id,
|
| 34 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")]
|
| 35 |
+
outputs = pipeline(
|
| 36 |
+
prompt,
|
| 37 |
+
max_new_tokens = max_new_tokens,
|
| 38 |
+
eos_token_id = terminators,
|
| 39 |
+
do_sample = True,
|
| 40 |
+
temperature = temperature + 0.1,
|
| 41 |
+
top_p = 0.9,)
|
| 42 |
+
return outputs[0]["generated_text"][len(prompt):]
|
| 43 |
|
| 44 |
"""
|
| 45 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 46 |
"""
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
+
chat_function,
|
| 49 |
+
textbox=gr.Textbox(placeholder="Enter message here", container=False, scale = 7),
|
| 50 |
+
chatbot=gr.Chatbot(height=400),
|
| 51 |
additional_inputs=[
|
| 52 |
+
gr.Textbox("You are helpful AI", label="System Prompt"),
|
| 53 |
+
gr.Slider(500,4000, label="Max New Tokens"),
|
| 54 |
+
gr.Slider(0,1, label="Temperature")
|
| 55 |
+
]
|
| 56 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|