Spaces:
Sleeping
Sleeping
eronariodito
commited on
Trying with existing transformer
Browse files
app.py
CHANGED
@@ -1,10 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
def respond(
|
@@ -15,8 +20,8 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
@@ -25,24 +30,34 @@ def respond(
|
|
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 +74,5 @@ 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 model and tokenizer directly
|
6 |
+
model_name = "jdowling/lora_model"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Move the model to the appropriate device (GPU if available, else CPU)
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model.to(device)
|
13 |
|
14 |
|
15 |
def respond(
|
|
|
20 |
temperature,
|
21 |
top_p,
|
22 |
):
|
23 |
+
# Prepare prompt with history
|
24 |
messages = [{"role": "system", "content": system_message}]
|
|
|
25 |
for val in history:
|
26 |
if val[0]:
|
27 |
messages.append({"role": "user", "content": val[0]})
|
|
|
30 |
|
31 |
messages.append({"role": "user", "content": message})
|
32 |
|
33 |
+
# Convert conversation into a single input string
|
34 |
+
prompt = f"{system_message}\n"
|
35 |
+
for turn in messages[1:]:
|
36 |
+
if turn["role"] == "user":
|
37 |
+
prompt += f"User: {turn['content']}\n"
|
38 |
+
elif turn["role"] == "assistant":
|
39 |
+
prompt += f"Assistant: {turn['content']}\n"
|
40 |
+
prompt += "Assistant:"
|
41 |
+
|
42 |
+
# Tokenize input
|
43 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
44 |
|
45 |
+
# Generate response
|
46 |
+
output = model.generate(
|
47 |
+
inputs["input_ids"],
|
48 |
+
max_length=inputs["input_ids"].shape[1] + max_tokens,
|
49 |
temperature=temperature,
|
50 |
top_p=top_p,
|
51 |
+
pad_token_id=tokenizer.eos_token_id,
|
52 |
+
)
|
53 |
|
54 |
+
# Decode response and extract the new assistant message
|
55 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
56 |
+
response = response[len(prompt):].strip() # Strip the input part from the response
|
57 |
+
|
58 |
+
yield response
|
59 |
|
60 |
|
|
|
|
|
|
|
61 |
demo = gr.ChatInterface(
|
62 |
respond,
|
63 |
additional_inputs=[
|
|
|
74 |
],
|
75 |
)
|
76 |
|
|
|
77 |
if __name__ == "__main__":
|
78 |
demo.launch()
|