Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 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 |
-
|
| 12 |
-
history
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 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 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
|
|
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize the client with your desired model
|
|
|
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# Define the system prompt as an AI Dermatologist
|
| 8 |
+
def format_prompt(message, history):
|
| 9 |
+
prompt = "<s>"
|
| 10 |
+
# Start the conversation with a system message
|
| 11 |
+
prompt += "[INST] You are an AI Dermatologist designed to assist users with skin and hair care by providing text.[/INST]"
|
| 12 |
+
for user_prompt, bot_response in history:
|
| 13 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 14 |
+
prompt += f" {bot_response}</s> "
|
| 15 |
+
prompt += f"[INST] {message} [/INST]"
|
| 16 |
+
return prompt
|
| 17 |
|
| 18 |
+
# Function to generate responses with the AI Dermatologist context
|
| 19 |
+
def generate(
|
| 20 |
+
prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
):
|
| 22 |
+
temperature = float(temperature)
|
| 23 |
+
if temperature < 1e-2:
|
| 24 |
+
temperature = 1e-2
|
| 25 |
+
top_p = float(top_p)
|
| 26 |
|
| 27 |
+
generate_kwargs = dict(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
temperature=temperature,
|
| 29 |
+
max_new_tokens=max_new_tokens,
|
| 30 |
top_p=top_p,
|
| 31 |
+
repetition_penalty=repetition_penalty,
|
| 32 |
+
do_sample=True,
|
| 33 |
+
seed=42,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 37 |
|
| 38 |
+
stream = client.text_generation(
|
| 39 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
|
| 40 |
+
)
|
| 41 |
+
output = ""
|
| 42 |
|
| 43 |
+
for response in stream:
|
| 44 |
+
output += response.token.text
|
| 45 |
+
yield output
|
| 46 |
+
return output
|
| 47 |
|
| 48 |
+
# Customizable input controls for the chatbot interface
|
| 49 |
+
additional_inputs = [
|
| 50 |
+
gr.Slider(
|
| 51 |
+
label="Temperature",
|
| 52 |
+
value=0.9,
|
| 53 |
+
minimum=0.0,
|
| 54 |
+
maximum=1.0,
|
| 55 |
+
step=0.05,
|
| 56 |
+
interactive=True,
|
| 57 |
+
info="Higher values produce more diverse outputs",
|
| 58 |
+
),
|
| 59 |
+
gr.Slider(
|
| 60 |
+
label="Max new tokens",
|
| 61 |
+
value=256,
|
| 62 |
+
minimum=0,
|
| 63 |
+
maximum=1048,
|
| 64 |
+
step=64,
|
| 65 |
+
interactive=True,
|
| 66 |
+
info="The maximum numbers of new tokens",
|
| 67 |
+
),
|
| 68 |
+
gr.Slider(
|
| 69 |
+
label="Top-p (nucleus sampling)",
|
| 70 |
+
value=0.90,
|
| 71 |
+
minimum=0.0,
|
| 72 |
+
maximum=1,
|
| 73 |
+
step=0.05,
|
| 74 |
+
interactive=True,
|
| 75 |
+
info="Higher values sample more low-probability tokens",
|
| 76 |
+
),
|
| 77 |
+
gr.Slider(
|
| 78 |
+
label="Repetition penalty",
|
| 79 |
+
value=1.2,
|
| 80 |
+
minimum=1.0,
|
| 81 |
+
maximum=2.0,
|
| 82 |
+
step=0.05,
|
| 83 |
+
interactive=True,
|
| 84 |
+
info="Penalize repeated tokens",
|
| 85 |
+
)
|
| 86 |
+
]
|
| 87 |
|
| 88 |
+
# Define the chatbot interface with the starting system message as AI Dermatologist
|
| 89 |
+
gr.ChatInterface(
|
| 90 |
+
fn=generate,
|
| 91 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
|
| 92 |
+
additional_inputs=additional_inputs,
|
| 93 |
+
title="AI Dermatologist"
|
| 94 |
+
).launch(show_api=False)
|
| 95 |
|
| 96 |
+
# Load your model after launching the interface
|
| 97 |
+
gr.load("models/Bhaskar2611/Capstone").launch()
|