Spaces:
Sleeping
Sleeping
Committing until it works
Browse files
app.py
CHANGED
@@ -1,10 +1,17 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def respond(
|
@@ -27,16 +34,24 @@ def respond(
|
|
27 |
|
28 |
response = ""
|
29 |
|
30 |
-
|
31 |
-
messages,
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
):
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
response += token
|
40 |
yield response
|
41 |
|
42 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import TextStreamer
|
3 |
+
|
4 |
+
# Load model directly
|
5 |
+
from transformers import AutoModel, AutoTokenizer
|
6 |
|
7 |
"""
|
8 |
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
|
9 |
"""
|
10 |
+
|
11 |
+
model_name_or_path = "samlama111/lora_model"
|
12 |
+
|
13 |
+
model = AutoModel.from_pretrained(model_name_or_path)
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
|
15 |
|
16 |
|
17 |
def respond(
|
|
|
34 |
|
35 |
response = ""
|
36 |
|
37 |
+
inputs = tokenizer.apply_chat_template(
|
38 |
+
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
|
39 |
+
)
|
40 |
+
|
41 |
+
text_streamer = TextStreamer(tokenizer)
|
42 |
+
# TODO: Doesn't stream ATM
|
43 |
+
for message in model.generate(
|
44 |
+
input_ids=inputs, streamer=text_streamer, max_new_tokens=1024, use_cache=True
|
45 |
):
|
46 |
+
# Decode the tensor to a string
|
47 |
+
decoded_message = tokenizer.decode(message, skip_special_tokens=True)
|
48 |
+
|
49 |
+
# Manually getting the response
|
50 |
+
response = decoded_message.split("assistant")[
|
51 |
+
-1
|
52 |
+
].strip() # Extract only the assistant's response
|
53 |
+
print(response)
|
54 |
|
|
|
55 |
yield response
|
56 |
|
57 |
|