Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -3,40 +3,33 @@ import gradio as gr
|
|
3 |
import torch
|
4 |
import time
|
5 |
|
6 |
-
# Load model and tokenizer
|
7 |
model = GPT2LMHeadModel.from_pretrained("GBhaveshKumar/ConvoAI")
|
8 |
tokenizer = AutoTokenizer.from_pretrained("GBhaveshKumar/ConvoAI")
|
9 |
|
10 |
-
|
11 |
-
device = torch.device("cpu")
|
12 |
model = model.to(device)
|
13 |
|
14 |
-
# Response generator
|
15 |
def respond(message, history):
|
16 |
-
# Simple 1-turn prompt (faster for CPU)
|
17 |
context = f"A: {message}\nB:"
|
18 |
-
inputs = tokenizer(context, return_tensors="pt",
|
19 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
20 |
|
21 |
-
# Measure response time
|
22 |
start_time = time.time()
|
23 |
outputs = model.generate(
|
24 |
**inputs,
|
25 |
-
max_new_tokens=
|
26 |
do_sample=False,
|
27 |
pad_token_id=tokenizer.eos_token_id
|
28 |
)
|
29 |
duration = time.time() - start_time
|
30 |
print(f"⏱️ Response time: {duration:.2f} seconds")
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
history.append((message, reply))
|
37 |
return history
|
38 |
|
39 |
-
# Gradio UI
|
40 |
with gr.Blocks() as demo:
|
41 |
gr.Markdown("<h1 style='text-align: center; color: #2e8b57;'>🤖 ConvoAI</h1>")
|
42 |
gr.Markdown("<p style='text-align: center; font-size: 18px;'>Conversational AI trained using the DailyDialog dataset. Engage in meaningful and natural conversations!</p>")
|
|
|
3 |
import torch
|
4 |
import time
|
5 |
|
|
|
6 |
model = GPT2LMHeadModel.from_pretrained("GBhaveshKumar/ConvoAI")
|
7 |
tokenizer = AutoTokenizer.from_pretrained("GBhaveshKumar/ConvoAI")
|
8 |
|
9 |
+
device = torch.device("cpu")
|
|
|
10 |
model = model.to(device)
|
11 |
|
|
|
12 |
def respond(message, history):
|
|
|
13 |
context = f"A: {message}\nB:"
|
14 |
+
inputs = tokenizer(context, return_tensors="pt", truncation=True, max_length=128)
|
15 |
inputs = {k: v.to(device) for k, v in inputs.items()}
|
16 |
|
|
|
17 |
start_time = time.time()
|
18 |
outputs = model.generate(
|
19 |
**inputs,
|
20 |
+
max_new_tokens=32,
|
21 |
do_sample=False,
|
22 |
pad_token_id=tokenizer.eos_token_id
|
23 |
)
|
24 |
duration = time.time() - start_time
|
25 |
print(f"⏱️ Response time: {duration:.2f} seconds")
|
26 |
|
27 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
+
reply = decoded[len(context):].strip().split("\n")[0]
|
29 |
+
|
|
|
30 |
history.append((message, reply))
|
31 |
return history
|
32 |
|
|
|
33 |
with gr.Blocks() as demo:
|
34 |
gr.Markdown("<h1 style='text-align: center; color: #2e8b57;'>🤖 ConvoAI</h1>")
|
35 |
gr.Markdown("<p style='text-align: center; font-size: 18px;'>Conversational AI trained using the DailyDialog dataset. Engage in meaningful and natural conversations!</p>")
|