Spaces:
Sleeping
Sleeping
sreyanghosh
commited on
Commit
·
4f9b1d8
1
Parent(s):
6647d86
update/modified app.py
Browse files- app.py +36 -27
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,12 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -15,34 +23,36 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
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 +69,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 |
+
from peft import PeftModel
|
4 |
|
5 |
+
# Load the model and tokenizer
|
6 |
+
def load_model():
|
7 |
+
base_model_name = "unsloth/llama-3.2-1b-instruct-bnb-4bit" # Replace with your base model name
|
8 |
+
lora_model_name = "sreyanghosh/lora_model" # Replace with your LoRA model path
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map="auto")
|
11 |
+
model = PeftModel.from_pretrained(model, lora_model_name)
|
12 |
+
model.eval()
|
13 |
+
return tokenizer, model
|
14 |
|
15 |
+
tokenizer, model = load_model()
|
16 |
|
17 |
+
# Define the respond function
|
18 |
def respond(
|
19 |
message,
|
20 |
history: list[tuple[str, str]],
|
|
|
23 |
temperature,
|
24 |
top_p,
|
25 |
):
|
26 |
+
# Prepare the conversation history
|
27 |
messages = [{"role": "system", "content": system_message}]
|
28 |
+
for user_input, bot_response in history:
|
29 |
+
if user_input:
|
30 |
+
messages.append({"role": "user", "content": user_input})
|
31 |
+
if bot_response:
|
32 |
+
messages.append({"role": "assistant", "content": bot_response})
|
|
|
|
|
33 |
messages.append({"role": "user", "content": message})
|
34 |
|
35 |
+
# Format the input for the model
|
36 |
+
conversation_text = "\n".join(
|
37 |
+
f"{msg['role']}: {msg['content']}" for msg in messages
|
38 |
+
)
|
39 |
+
inputs = tokenizer(conversation_text, return_tensors="pt", truncation=True)
|
40 |
+
|
41 |
+
# Generate the model's response
|
42 |
+
outputs = model.generate(
|
43 |
+
inputs.input_ids,
|
44 |
+
max_length=len(inputs.input_ids[0]) + max_tokens,
|
45 |
temperature=temperature,
|
46 |
top_p=top_p,
|
47 |
+
pad_token_id=tokenizer.eos_token_id,
|
48 |
+
)
|
49 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
50 |
|
51 |
+
# Extract the new response
|
52 |
+
new_response = response[len(conversation_text):].strip()
|
53 |
+
yield new_response
|
54 |
|
55 |
+
# Gradio app configuration
|
|
|
|
|
|
|
56 |
demo = gr.ChatInterface(
|
57 |
respond,
|
58 |
additional_inputs=[
|
|
|
69 |
],
|
70 |
)
|
71 |
|
|
|
72 |
if __name__ == "__main__":
|
73 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
1 |
huggingface_hub==0.25.2
|
2 |
gradio
|
3 |
-
|
|
|
|
|
|
1 |
huggingface_hub==0.25.2
|
2 |
gradio
|
3 |
+
transformers
|
4 |
+
peft
|
5 |
+
torch
|