Spaces:
Sleeping
Sleeping
updated app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,123 @@
|
|
1 |
-
import
|
2 |
-
from huggingface_hub import
|
|
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
25 |
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
"""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from huggingface_hub import login
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, T5Tokenizer
|
4 |
+
from peft import PeftModel, PeftConfig
|
5 |
|
6 |
+
token = os.environ.get("token")
|
7 |
+
login(token)
|
8 |
+
print("login is succesful")
|
9 |
+
max_length=512
|
10 |
|
11 |
+
MODEL_NAME = "google/flan-t5-base"
|
12 |
+
tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME, token=token)
|
13 |
+
config = PeftConfig.from_pretrained("Orcawise/results")
|
14 |
+
base_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
15 |
+
model = PeftModel.from_pretrained(base_model, "Orcawise/results")
|
16 |
|
17 |
+
#gr.Interface.from_pipeline(pipe).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def generate_text(prompt, max_length=512):
|
20 |
+
"""Generates text using the PEFT model.
|
21 |
+
Args:
|
22 |
+
prompt (str): The user-provided prompt to start the generation.
|
23 |
+
Returns:
|
24 |
+
str: The generated text.
|
25 |
+
"""
|
26 |
|
|
|
27 |
|
28 |
+
# Preprocess the prompt
|
29 |
+
# inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
30 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
31 |
|
32 |
+
# Generate text using beam search
|
33 |
+
outputs = model.generate(
|
34 |
+
input_ids = inputs["input_ids"],
|
35 |
+
max_length=max_length,
|
36 |
+
num_beams=1
|
37 |
+
|
38 |
+
)
|
|
|
39 |
|
40 |
+
# Decode the generated tokens
|
41 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
+
print("show the generated text", generated_text)
|
43 |
+
return generated_text
|
44 |
|
45 |
+
#############
|
46 |
+
custom_css="""
|
47 |
+
.message.pending {
|
48 |
+
background: #A8C4D6;
|
49 |
+
}
|
50 |
+
/* Response message */
|
51 |
+
.message.bot.svelte-1s78gfg.message-bubble-border {
|
52 |
+
/* background: white; */
|
53 |
+
border-color: #266B99
|
54 |
+
}
|
55 |
+
/* User message */
|
56 |
+
.message.user.svelte-1s78gfg.message-bubble-border{
|
57 |
+
background: #9DDDF9;
|
58 |
+
border-color: #9DDDF9
|
59 |
+
|
60 |
+
}
|
61 |
+
/* For both user and response message as per the document */
|
62 |
+
span.md.svelte-8tpqd2.chatbot.prose p {
|
63 |
+
color: #266B99;
|
64 |
+
}
|
65 |
+
/* Chatbot comtainer */
|
66 |
+
.gradio-container{
|
67 |
+
/* background: #84D5F7 */
|
68 |
+
}
|
69 |
+
/* RED (Hex: #DB1616) for action buttons and links only */
|
70 |
+
.clear-btn {
|
71 |
+
background: #DB1616;
|
72 |
+
color: white;
|
73 |
+
}
|
74 |
+
/* #84D5F7 - Primary colours are set to be used for all sorts */
|
75 |
+
.submit-btn {
|
76 |
+
background: #266B99;
|
77 |
+
color: white;
|
78 |
+
}
|
79 |
"""
|
80 |
+
|
81 |
+
### working correctly but the welcoming message isnt rendering
|
82 |
+
with gr.Blocks(css=custom_css) as demo:
|
83 |
+
chatbot = gr.Chatbot()
|
84 |
+
msg = gr.Textbox(placeholder="Ask your question...") # Add placeholder text
|
85 |
+
submit_button = gr.Button("Submit", elem_classes="submit-btn")
|
86 |
+
clear = gr.Button("Clear", elem_classes="clear-btn")
|
87 |
+
|
88 |
+
|
89 |
+
def user(user_message, history):
|
90 |
+
return "", history + [[user_message, None]]
|
91 |
+
|
92 |
+
|
93 |
+
def bot(history):
|
94 |
+
history[-1][1] = "" # Update the last bot message (welcome message or response)
|
95 |
+
if len(history) < 0: # Check if it's the first interaction
|
96 |
+
bot_message = "Hi there! How can I help you today?"
|
97 |
+
history.append([None, bot_message]) # Add welcome message to history
|
98 |
+
for character in bot_message:
|
99 |
+
history[-1][1] += character
|
100 |
+
yield history # Yield the updated history character by character
|
101 |
+
|
102 |
+
else:
|
103 |
+
previous_message = history[-1][0] # Access the previous user message
|
104 |
+
bot_message = generate_text(previous_message) # Generate response based on previous message
|
105 |
+
for character in bot_message:
|
106 |
+
history[-1][1] += character
|
107 |
+
yield history # Yield the updated history character by character
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
# Connect submit button to user and then bot functions
|
112 |
+
submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
113 |
+
bot, chatbot, chatbot
|
114 |
+
)
|
115 |
+
|
116 |
+
# Trigger user function on Enter key press (same chain as submit button)
|
117 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
118 |
+
bot, chatbot, chatbot
|
119 |
+
)
|
120 |
+
|
121 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
122 |
+
|
123 |
+
demo.launch()
|