Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
from huggingface_hub import InferenceClient
|
| 2 |
from unsloth import FastLanguageModel
|
| 3 |
import torch
|
| 4 |
import gradio as gr
|
|
@@ -6,91 +5,81 @@ import gradio as gr
|
|
| 6 |
"""
|
| 7 |
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
|
| 8 |
"""
|
| 9 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 10 |
model,tokenizer = FastLanguageModel.from_pretrained('./unified_model_v3')
|
| 11 |
|
| 12 |
-
def generate_response(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
max_tokens,
|
| 17 |
-
temperature,
|
| 18 |
-
top_p,
|
| 19 |
-
top_k
|
| 20 |
-
):
|
| 21 |
-
messages = [{"role": "system", "content": system_message}]
|
| 22 |
-
|
| 23 |
-
for val in history:
|
| 24 |
-
if val[0]:
|
| 25 |
-
messages.append({"role": "user", "content": val[0]})
|
| 26 |
-
if val[1]:
|
| 27 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
-
|
| 29 |
-
messages.append({"role": "user", "content": message})
|
| 30 |
-
|
| 31 |
-
response = ""
|
| 32 |
-
|
| 33 |
-
# inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 34 |
-
# with torch.no_grad():
|
| 35 |
-
# outputs = model.generate(**inputs,
|
| 36 |
-
# early_stopping=True,
|
| 37 |
-
# min_length=50,
|
| 38 |
-
# length_penalty=2,
|
| 39 |
-
# do_sample=True,
|
| 40 |
-
# max_new_tokens=max_tokens,
|
| 41 |
-
# top_p=top_p,
|
| 42 |
-
# top_k=top_k,
|
| 43 |
-
# temperature=temperature,
|
| 44 |
-
# repetition_penalty=1.2,
|
| 45 |
-
# num_return_sequences=1
|
| 46 |
-
# )
|
| 47 |
-
# response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 48 |
-
# yield response
|
| 49 |
-
# response = response.split("### Answer:")[-1]
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
for message in client.chat_completion(
|
| 53 |
-
messages=messages,
|
| 54 |
-
model=model,
|
| 55 |
-
repetition_penalty=1.2,
|
| 56 |
-
max_tokens=max_tokens,
|
| 57 |
-
stream=True,
|
| 58 |
-
temperature=temperature,
|
| 59 |
-
top_p=top_p,
|
| 60 |
-
):
|
| 61 |
-
token = message.choices[0].delta.content
|
| 62 |
-
|
| 63 |
-
response += token
|
| 64 |
-
yield response
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
"""
|
| 68 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 69 |
-
"""
|
| 70 |
-
demo = gr.ChatInterface(
|
| 71 |
-
generate_response,
|
| 72 |
-
additional_inputs=[
|
| 73 |
-
gr.Textbox(value="""Instruction:
|
| 74 |
Respond to the user's question. Don't include code in your response.
|
| 75 |
|
| 76 |
### Question:
|
| 77 |
{instruction}
|
| 78 |
|
| 79 |
Please provide a unique, concise, and non-repetitive answer
|
| 80 |
-
### Answer:"""
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
from unsloth import FastLanguageModel
|
| 2 |
import torch
|
| 3 |
import gradio as gr
|
|
|
|
| 5 |
"""
|
| 6 |
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
|
| 7 |
"""
|
|
|
|
| 8 |
model,tokenizer = FastLanguageModel.from_pretrained('./unified_model_v3')
|
| 9 |
|
| 10 |
+
def generate_response(instruction,chat_history):
|
| 11 |
+
"""Generates a response using your fine-tuned model."""
|
| 12 |
+
# FastLanguageModel.for_inference(model) # Enable native 2x faster inference within the function
|
| 13 |
+
prompt = f"""### Instruction:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
Respond to the user's question. Don't include code in your response.
|
| 15 |
|
| 16 |
### Question:
|
| 17 |
{instruction}
|
| 18 |
|
| 19 |
Please provide a unique, concise, and non-repetitive answer
|
| 20 |
+
### Answer:"""
|
| 21 |
+
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(**inputs,early_stopping=True,min_length=50,length_penalty=2,do_sample=True,max_new_tokens=300,
|
| 25 |
+
top_p=0.95,
|
| 26 |
+
top_k=50,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
repetition_penalty=1.2,
|
| 29 |
+
num_return_sequences=1
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
+
response = response.split("### Answer:")[-1]
|
| 34 |
+
return response
|
| 35 |
+
|
| 36 |
+
def update_chat_history(chat_history, user_message, bot_message):
|
| 37 |
+
"""Update chat history to maintain relevance and avoid excessive growth."""
|
| 38 |
+
chat_history['user'].append(user_message)
|
| 39 |
+
chat_history['bot'].append(bot_message)
|
| 40 |
+
# Keep only the last N interactions
|
| 41 |
+
if len(chat_history['user']) > 5:
|
| 42 |
+
chat_history['user'] = chat_history['user'][-5:]
|
| 43 |
+
chat_history['bot'] = chat_history['bot'][-5:]
|
| 44 |
+
return chat_history
|
| 45 |
+
|
| 46 |
+
def chatbot(input_text,chat_history):
|
| 47 |
+
messages = {
|
| 48 |
+
"user": [],
|
| 49 |
+
"bot": [],
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
for user_msg, bot_msg in chat_history:
|
| 53 |
+
messages["user"].append(user_msg)
|
| 54 |
+
messages["bot"].append(bot_msg)
|
| 55 |
+
|
| 56 |
+
bot_response = generate_response(input_text,messages)
|
| 57 |
+
chat_history.append(("User: " + input_text, bot_response))
|
| 58 |
+
messages = update_chat_history(messages, input_text, bot_response)
|
| 59 |
+
return "", chat_history
|
| 60 |
+
|
| 61 |
+
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown('## AILA INTERFACE DEMO')
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
|
| 66 |
+
user_input = gr.Textbox(
|
| 67 |
+
placeholder = "Type your message here...",
|
| 68 |
+
label = "Your Message",
|
| 69 |
+
lines = 1
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
submit_button = gr.Button('Submit')
|
| 73 |
+
|
| 74 |
+
chat_history = gr.Chatbot()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
submit_button.click(
|
| 79 |
+
chatbot,
|
| 80 |
+
inputs = [user_input,chat_history],
|
| 81 |
+
outputs = [user_input, chat_history]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
demo.launch()
|