Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,75 +7,87 @@ def load_llm():
|
|
7 |
Loads the GPT-2 model and tokenizer using the Hugging Face `transformers` library.
|
8 |
"""
|
9 |
try:
|
10 |
-
print("
|
11 |
-
model_name = 'gpt2' # Replace with your custom model
|
12 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
13 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
14 |
print("Model and tokenizer successfully loaded!")
|
15 |
return model, tokenizer
|
16 |
except Exception as e:
|
17 |
-
print(f"
|
18 |
return None, None
|
19 |
|
20 |
-
def generate_response(model, tokenizer, user_input
|
21 |
"""
|
22 |
-
Generates a response using the GPT-2 model
|
23 |
|
24 |
Args:
|
25 |
-
- model: The GPT-2 model.
|
26 |
-
- tokenizer: The corresponding
|
27 |
-
- user_input (str): The
|
28 |
-
- max_length (int): The maximum length of the generated output.
|
29 |
|
30 |
Returns:
|
31 |
-
- response (str): The
|
32 |
"""
|
33 |
try:
|
34 |
inputs = tokenizer.encode(user_input, return_tensors='pt')
|
35 |
-
outputs = model.generate(inputs, max_length=
|
36 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
return response
|
38 |
except Exception as e:
|
39 |
-
return f"
|
40 |
|
41 |
# Load the model and tokenizer
|
42 |
model, tokenizer = load_llm()
|
43 |
|
44 |
if model is None or tokenizer is None:
|
45 |
-
print("
|
46 |
else:
|
47 |
-
print("Model and tokenizer are ready
|
48 |
|
49 |
-
# Initialize the Hugging Face API client
|
50 |
client = InferenceClient()
|
51 |
|
52 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
53 |
"""
|
54 |
-
Handles the chatbot
|
55 |
-
to the Hugging Face Inference API
|
56 |
"""
|
57 |
-
print("
|
58 |
-
print("
|
59 |
-
print("
|
60 |
|
61 |
-
# Construct the conversation history with the system message
|
62 |
messages = [{"role": "system", "content": system_message}]
|
63 |
|
64 |
for user_msg, assistant_msg in history:
|
65 |
if user_msg:
|
66 |
-
print("Adding user message to
|
67 |
messages.append({"role": "user", "content": user_msg})
|
68 |
if assistant_msg:
|
69 |
-
print("Adding assistant message to
|
70 |
messages.append({"role": "assistant", "content": assistant_msg})
|
71 |
|
72 |
messages.append({"role": "user", "content": message})
|
73 |
-
print("
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Set up the Gradio ChatInterface
|
81 |
demo = gr.ChatInterface(
|
|
|
7 |
Loads the GPT-2 model and tokenizer using the Hugging Face `transformers` library.
|
8 |
"""
|
9 |
try:
|
10 |
+
print("Downloading or loading the GPT-2 model and tokenizer...")
|
11 |
+
model_name = 'gpt2' # Replace with your custom model if available
|
12 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
13 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
14 |
print("Model and tokenizer successfully loaded!")
|
15 |
return model, tokenizer
|
16 |
except Exception as e:
|
17 |
+
print(f"An error occurred while loading the model: {e}")
|
18 |
return None, None
|
19 |
|
20 |
+
def generate_response(model, tokenizer, user_input):
|
21 |
"""
|
22 |
+
Generates a response using the GPT-2 model and tokenizer.
|
23 |
|
24 |
Args:
|
25 |
+
- model: The loaded GPT-2 model.
|
26 |
+
- tokenizer: The tokenizer corresponding to the GPT-2 model.
|
27 |
+
- user_input (str): The input question from the user.
|
|
|
28 |
|
29 |
Returns:
|
30 |
+
- response (str): The generated response.
|
31 |
"""
|
32 |
try:
|
33 |
inputs = tokenizer.encode(user_input, return_tensors='pt')
|
34 |
+
outputs = model.generate(inputs, max_length=512, num_return_sequences=1)
|
35 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
36 |
return response
|
37 |
except Exception as e:
|
38 |
+
return f"An error occurred during response generation: {e}"
|
39 |
|
40 |
# Load the model and tokenizer
|
41 |
model, tokenizer = load_llm()
|
42 |
|
43 |
if model is None or tokenizer is None:
|
44 |
+
print("Model and/or tokenizer loading failed.")
|
45 |
else:
|
46 |
+
print("Model and tokenizer are ready for use.")
|
47 |
|
48 |
+
# Initialize the Hugging Face API client (ensure it’s correctly set up)
|
49 |
client = InferenceClient()
|
50 |
|
51 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
52 |
"""
|
53 |
+
Handles interaction with the chatbot by sending the conversation history
|
54 |
+
and system message to the Hugging Face Inference API.
|
55 |
"""
|
56 |
+
print("Starting respond function")
|
57 |
+
print("Received message:", message)
|
58 |
+
print("Conversation history:", history)
|
59 |
|
|
|
60 |
messages = [{"role": "system", "content": system_message}]
|
61 |
|
62 |
for user_msg, assistant_msg in history:
|
63 |
if user_msg:
|
64 |
+
print("Adding user message to messages:", user_msg)
|
65 |
messages.append({"role": "user", "content": user_msg})
|
66 |
if assistant_msg:
|
67 |
+
print("Adding assistant message to messages:", assistant_msg)
|
68 |
messages.append({"role": "assistant", "content": assistant_msg})
|
69 |
|
70 |
messages.append({"role": "user", "content": message})
|
71 |
+
print("Final message list for the model:", messages)
|
72 |
|
73 |
+
response = ""
|
74 |
+
try:
|
75 |
+
for message in client.chat_completion(
|
76 |
+
messages,
|
77 |
+
max_tokens=max_tokens,
|
78 |
+
stream=True,
|
79 |
+
temperature=temperature,
|
80 |
+
top_p=top_p,
|
81 |
+
):
|
82 |
+
token = message['choices'][0]['delta']['content']
|
83 |
+
response += token
|
84 |
+
print("Token received:", token)
|
85 |
+
yield response
|
86 |
+
except Exception as e:
|
87 |
+
print("An error occurred:", e)
|
88 |
+
yield f"An error occurred: {e}"
|
89 |
+
|
90 |
+
print("Response generation completed")
|
91 |
|
92 |
# Set up the Gradio ChatInterface
|
93 |
demo = gr.ChatInterface(
|