Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
def respond(
|
11 |
message,
|
@@ -15,34 +15,38 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
for
|
21 |
-
if
|
22 |
-
messages.append({"role": "user", "content":
|
23 |
-
if
|
24 |
-
messages.append({"role": "assistant", "content":
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
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 |
respond,
|
48 |
additional_inputs=[
|
@@ -59,6 +63,6 @@ demo = gr.ChatInterface(
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Load your custom model and tokenizer
|
6 |
+
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct" # Replace with your model's Hugging Face repo ID or local path
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map="auto")
|
9 |
|
10 |
def respond(
|
11 |
message,
|
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
+
# Prepare the chat history
|
19 |
messages = [{"role": "system", "content": system_message}]
|
20 |
|
21 |
+
for user_msg, assistant_msg in history:
|
22 |
+
if user_msg:
|
23 |
+
messages.append({"role": "user", "content": user_msg})
|
24 |
+
if assistant_msg:
|
25 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
26 |
|
27 |
messages.append({"role": "user", "content": message})
|
28 |
|
29 |
+
# Format the input for the model
|
30 |
+
input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
|
31 |
|
32 |
+
# Generate a response
|
33 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
34 |
+
outputs = model.generate(
|
35 |
+
inputs.input_ids,
|
36 |
+
max_new_tokens=max_tokens,
|
37 |
temperature=temperature,
|
38 |
top_p=top_p,
|
39 |
+
do_sample=True,
|
40 |
+
)
|
41 |
+
|
42 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
43 |
|
44 |
+
# Extract only the assistant's response
|
45 |
+
assistant_response = response.split("assistant:")[-1].strip()
|
46 |
+
yield assistant_response
|
47 |
|
48 |
|
49 |
+
# Create the Gradio interface
|
|
|
|
|
50 |
demo = gr.ChatInterface(
|
51 |
respond,
|
52 |
additional_inputs=[
|
|
|
63 |
],
|
64 |
)
|
65 |
|
66 |
+
# Launch the app
|
67 |
if __name__ == "__main__":
|
68 |
+
demo.launch()
|