Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,10 @@
|
|
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("Aksh1t/mistral-7b-oig-unsloth-merged")
|
8 |
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -15,33 +14,32 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
19 |
-
|
20 |
-
for
|
21 |
-
if
|
22 |
-
|
23 |
-
if
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
@@ -58,6 +56,5 @@ demo = gr.ChatInterface(
|
|
58 |
],
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load the tokenizer and model
|
5 |
+
model_name = "Aksh1t/mistral-7b-oig-unsloth-merged"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
8 |
|
9 |
def respond(
|
10 |
message,
|
|
|
14 |
temperature,
|
15 |
top_p,
|
16 |
):
|
17 |
+
# Construct the prompt
|
18 |
+
prompt = system_message + "\n"
|
19 |
+
for user_msg, assistant_msg in history:
|
20 |
+
if user_msg:
|
21 |
+
prompt += f"User: {user_msg}\n"
|
22 |
+
if assistant_msg:
|
23 |
+
prompt += f"Assistant: {assistant_msg}\n"
|
24 |
+
prompt += f"User: {message}\nAssistant:"
|
25 |
+
|
26 |
+
# Encode the prompt and generate a response
|
27 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
28 |
+
outputs = model.generate(
|
29 |
+
inputs.input_ids,
|
30 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
31 |
temperature=temperature,
|
32 |
top_p=top_p,
|
33 |
+
do_sample=True
|
34 |
+
)
|
35 |
+
|
36 |
+
# Decode the generated response
|
37 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
|
39 |
+
# Extract the assistant's reply
|
40 |
+
assistant_reply = response.split("Assistant:")[-1].strip()
|
41 |
+
yield assistant_reply
|
42 |
|
|
|
|
|
|
|
43 |
demo = gr.ChatInterface(
|
44 |
respond,
|
45 |
additional_inputs=[
|
|
|
56 |
],
|
57 |
)
|
58 |
|
|
|
59 |
if __name__ == "__main__":
|
60 |
+
demo.launch()
|