tell me how you fail specifically
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
import torch
|
|
|
4 |
|
5 |
# Load model and tokenizer from Hugging Face Hub
|
6 |
model_name = "Electricarchmage/cookbookgpt"
|
@@ -11,7 +12,7 @@ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
|
11 |
tokenizer.pad_token = tokenizer.eos_token
|
12 |
tokenizer.padding_side = 'left'
|
13 |
|
14 |
-
# Define the respond function
|
15 |
def respond(
|
16 |
message,
|
17 |
history: list[tuple[str, str]],
|
@@ -23,7 +24,6 @@ def respond(
|
|
23 |
# Preparing the messages for context (the history and the new message)
|
24 |
messages = [{"role": "system", "content": system_message}]
|
25 |
|
26 |
-
# Convert history to the required format with 'role' and 'content'
|
27 |
for val in history:
|
28 |
if val[0]:
|
29 |
messages.append({"role": "user", "content": val[0]})
|
@@ -32,21 +32,28 @@ def respond(
|
|
32 |
|
33 |
messages.append({"role": "user", "content": message})
|
34 |
|
35 |
-
# Tokenize the input
|
36 |
inputs = tokenizer([msg["content"] for msg in messages], return_tensors="pt", padding=True, truncation=True)
|
37 |
-
attention_mask = inputs.get('attention_mask', torch.ones_like(inputs['input_ids']))
|
|
|
|
|
38 |
|
39 |
# Generate output tokens
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Decode the output tokens into text
|
52 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
@@ -54,7 +61,8 @@ def respond(
|
|
54 |
# Extract only the assistant's reply
|
55 |
assistant_reply = response.split("Assistant:")[-1].strip()
|
56 |
|
57 |
-
|
|
|
58 |
|
59 |
# Define the Gradio interface
|
60 |
demo = gr.ChatInterface(
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
import torch
|
4 |
+
import time
|
5 |
|
6 |
# Load model and tokenizer from Hugging Face Hub
|
7 |
model_name = "Electricarchmage/cookbookgpt"
|
|
|
12 |
tokenizer.pad_token = tokenizer.eos_token
|
13 |
tokenizer.padding_side = 'left'
|
14 |
|
15 |
+
# Define the respond function with logging for debugging
|
16 |
def respond(
|
17 |
message,
|
18 |
history: list[tuple[str, str]],
|
|
|
24 |
# Preparing the messages for context (the history and the new message)
|
25 |
messages = [{"role": "system", "content": system_message}]
|
26 |
|
|
|
27 |
for val in history:
|
28 |
if val[0]:
|
29 |
messages.append({"role": "user", "content": val[0]})
|
|
|
32 |
|
33 |
messages.append({"role": "user", "content": message})
|
34 |
|
35 |
+
# Tokenize the input
|
36 |
inputs = tokenizer([msg["content"] for msg in messages], return_tensors="pt", padding=True, truncation=True)
|
37 |
+
attention_mask = inputs.get('attention_mask', torch.ones_like(inputs['input_ids']))
|
38 |
+
|
39 |
+
start_time = time.time() # Start the timer
|
40 |
|
41 |
# Generate output tokens
|
42 |
+
try:
|
43 |
+
output = model.generate(
|
44 |
+
inputs["input_ids"],
|
45 |
+
attention_mask=attention_mask,
|
46 |
+
max_length=max_tokens + len(inputs["input_ids"][0]),
|
47 |
+
temperature=temperature,
|
48 |
+
top_p=top_p,
|
49 |
+
num_return_sequences=1,
|
50 |
+
do_sample=True,
|
51 |
+
no_repeat_ngram_size=2,
|
52 |
+
)
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error during generation: {str(e)}"
|
55 |
+
|
56 |
+
generation_time = time.time() - start_time # Time taken for generation
|
57 |
|
58 |
# Decode the output tokens into text
|
59 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
61 |
# Extract only the assistant's reply
|
62 |
assistant_reply = response.split("Assistant:")[-1].strip()
|
63 |
|
64 |
+
# Add generation time in the response for debugging
|
65 |
+
return f"Response: {assistant_reply}\nGeneration time: {generation_time:.2f} seconds"
|
66 |
|
67 |
# Define the Gradio interface
|
68 |
demo = gr.ChatInterface(
|