Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,77 @@
|
|
1 |
|
|
|
|
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
-
import gradio as gr
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
# Load the tokenizer and model from Hugging Face
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
11 |
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
model.to(device)
|
14 |
|
15 |
-
#
|
16 |
-
def generate_response(
|
17 |
-
#
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True, max_length=512).to(device)
|
|
|
|
|
21 |
outputs = model.generate(
|
22 |
inputs['input_ids'],
|
23 |
-
max_length=
|
24 |
num_return_sequences=1,
|
25 |
pad_token_id=tokenizer.eos_token_id,
|
26 |
-
num_beams=num_beams,
|
27 |
temperature=temperature,
|
28 |
top_p=top_p,
|
29 |
-
repetition_penalty=repetition_penalty,
|
30 |
early_stopping=True
|
31 |
)
|
32 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
33 |
|
34 |
-
# Clean up the response
|
35 |
-
response = response.
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
],
|
53 |
-
|
|
|
54 |
)
|
55 |
|
56 |
-
|
57 |
-
|
|
|
1 |
|
2 |
+
model_name = 'redael/model_udc'
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
import torch
|
|
|
7 |
|
8 |
+
# Load your model and tokenizer from Hugging Face
|
9 |
+
print("l.......")
|
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
+
print("done")
|
13 |
|
14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
15 |
model.to(device)
|
16 |
|
17 |
+
# Function to generate response
|
18 |
+
def generate_response(message, history, system_message, max_tokens, temperature, top_p):
|
19 |
+
# Prepare the conversation history
|
20 |
+
messages = [{"role": "system", "content": system_message}]
|
21 |
+
|
22 |
+
for user_msg, bot_msg in history:
|
23 |
+
if user_msg:
|
24 |
+
messages.append({"role": "user", "content": user_msg})
|
25 |
+
if bot_msg:
|
26 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
27 |
+
|
28 |
+
messages.append({"role": "user", "content": message})
|
29 |
+
|
30 |
+
# Tokenize and prepare the input
|
31 |
+
prompt = "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages])
|
32 |
inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True, max_length=512).to(device)
|
33 |
+
|
34 |
+
# Generate the response
|
35 |
outputs = model.generate(
|
36 |
inputs['input_ids'],
|
37 |
+
max_length=max_tokens,
|
38 |
num_return_sequences=1,
|
39 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
40 |
temperature=temperature,
|
41 |
top_p=top_p,
|
|
|
42 |
early_stopping=True
|
43 |
)
|
44 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
45 |
|
46 |
+
# Clean up the response
|
47 |
+
response = response.split("Assistant:")[-1].strip()
|
48 |
+
response_lines = response.split('\n')
|
49 |
+
clean_response = []
|
50 |
+
for line in response_lines:
|
51 |
+
if "User:" not in line and "Assistant:" not in line:
|
52 |
+
clean_response.append(line)
|
53 |
+
response = ' '.join(clean_response)
|
54 |
+
|
55 |
+
return [(message, response)]
|
56 |
|
57 |
+
# Create the Gradio chat interface
|
58 |
+
demo = gr.ChatInterface(
|
59 |
+
fn=generate_response,
|
60 |
+
additional_inputs=[
|
61 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
62 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
63 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
64 |
+
gr.Slider(
|
65 |
+
minimum=0.1,
|
66 |
+
maximum=1.0,
|
67 |
+
value=0.95,
|
68 |
+
step=0.05,
|
69 |
+
label="Top-p (nucleus sampling)",
|
70 |
+
),
|
71 |
],
|
72 |
+
title="Chatbot",
|
73 |
+
description="Ask anything to the chatbot."
|
74 |
)
|
75 |
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.launch()
|