Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -18,11 +18,11 @@ base_model = AutoModelForCausalLM.from_pretrained(
|
|
18 |
)
|
19 |
|
20 |
# Load fine-tuned LoRA weights
|
21 |
-
fine_tuned_model_path = "
|
22 |
model = PeftModel.from_pretrained(base_model, fine_tuned_model_path)
|
23 |
model = model.merge_and_unload() # Merge LoRA weights
|
24 |
|
25 |
-
# Load tokenizer
|
26 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
27 |
tokenizer.pad_token = tokenizer.eos_token
|
28 |
tokenizer.padding_side = "right"
|
@@ -31,23 +31,35 @@ tokenizer.padding_side = "right"
|
|
31 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=500)
|
32 |
|
33 |
|
34 |
-
# β
Chatbot Function
|
35 |
def chat(user_input, history=[]):
|
36 |
-
"""Generates a response from the fine-tuned Phi-2 model."""
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
response = generator(prompt, max_length=500, do_sample=True)
|
|
|
|
|
39 |
answer = response[0]["generated_text"].split("### Assistant:\n")[-1].strip()
|
40 |
|
41 |
-
#
|
42 |
history.append((user_input, answer))
|
43 |
-
|
|
|
44 |
|
45 |
|
46 |
# β
Create Gradio Chat Interface
|
47 |
chatbot = gr.ChatInterface(
|
48 |
fn=chat,
|
49 |
-
title="Fine-Tuned Phi-2 Chat Assistant",
|
50 |
-
description="π Chat with a fine-tuned Phi-2 model.
|
51 |
theme="compact",
|
52 |
)
|
53 |
|
|
|
18 |
)
|
19 |
|
20 |
# Load fine-tuned LoRA weights
|
21 |
+
fine_tuned_model_path = "piyushgrover/phi2-qlora-adapter-s18erav3"
|
22 |
model = PeftModel.from_pretrained(base_model, fine_tuned_model_path)
|
23 |
model = model.merge_and_unload() # Merge LoRA weights
|
24 |
|
25 |
+
# β
Load tokenizer
|
26 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
27 |
tokenizer.pad_token = tokenizer.eos_token
|
28 |
tokenizer.padding_side = "right"
|
|
|
31 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=500)
|
32 |
|
33 |
|
34 |
+
# β
Chatbot Function with Conversation History
|
35 |
def chat(user_input, history=[]):
|
36 |
+
"""Generates a response from the fine-tuned Phi-2 model with conversation memory."""
|
37 |
+
|
38 |
+
# Format the chat history properly
|
39 |
+
formatted_history = ""
|
40 |
+
for usr, bot in history:
|
41 |
+
formatted_history += f"\n\n### User:\n{usr}\n\n### Assistant:\n{bot}"
|
42 |
+
|
43 |
+
# Append the latest user message
|
44 |
+
prompt = f"{formatted_history}\n\n### User:\n{user_input}\n\n### Assistant:\n"
|
45 |
+
|
46 |
+
# Generate response
|
47 |
response = generator(prompt, max_length=500, do_sample=True)
|
48 |
+
|
49 |
+
# Extract only the model's generated response
|
50 |
answer = response[0]["generated_text"].split("### Assistant:\n")[-1].strip()
|
51 |
|
52 |
+
# Update conversation history
|
53 |
history.append((user_input, answer))
|
54 |
+
|
55 |
+
return "", history # Return empty input and updated history
|
56 |
|
57 |
|
58 |
# β
Create Gradio Chat Interface
|
59 |
chatbot = gr.ChatInterface(
|
60 |
fn=chat,
|
61 |
+
title="Fine-Tuned Phi-2 Conversational Chat Assistant",
|
62 |
+
description="π Chat with a fine-tuned Phi-2 model. It remembers the conversation!",
|
63 |
theme="compact",
|
64 |
)
|
65 |
|