Sanchit2207 commited on
Commit
cb89037
·
verified ·
1 Parent(s): f62af45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -24
app.py CHANGED
@@ -1,42 +1,62 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM
2
  import torch
 
 
 
3
 
4
- # Load same or different models for each agent
5
- tokenizer1 = AutoTokenizer.from_pretrained("gpt2")
6
- model1 = AutoModelForCausalLM.from_pretrained("gpt2")
7
-
8
- tokenizer2 = AutoTokenizer.from_pretrained("gpt2-medium")
9
- model2 = AutoModelForCausalLM.from_pretrained("gpt2-medium")
10
 
11
- tokenizer3 = AutoTokenizer.from_pretrained("gpt2-large")
12
- model3 = AutoModelForCausalLM.from_pretrained("gpt2-large")
 
 
 
 
 
 
 
 
13
 
14
- device = "cuda" if torch.cuda.is_available() else "cpu"
15
- model1, model2, model3 = model1.to(device), model2.to(device), model3.to(device)
 
16
 
 
17
  def generate_response(model, tokenizer, prompt):
18
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
19
- outputs = model.generate(inputs["input_ids"], max_length=100, pad_token_id=tokenizer.eos_token_id)
 
 
 
 
 
 
 
 
20
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
21
 
22
- import gradio as gr
23
-
24
  def multi_agent_chat(user_input):
25
- res1 = generate_response(model1, tokenizer1, user_input)
26
- res2 = generate_response(model2, tokenizer2, user_input)
27
- res3 = generate_response(model3, tokenizer3, user_input)
28
-
29
- return res1, res2, res3
30
-
 
 
 
 
31
  interface = gr.Interface(
32
  fn=multi_agent_chat,
33
  inputs=gr.Textbox(lines=2, placeholder="Ask something..."),
34
  outputs=[
35
  gr.Textbox(label="Agent 1 (GPT-2)"),
36
- gr.Textbox(label="Agent 2 (GPT-2 Medium)"),
37
- gr.Textbox(label="Agent 3 (GPT-2 Large)")
38
  ],
39
- title="3-Agent AI Chatbot"
 
40
  )
41
 
42
  interface.launch()
 
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import gradio as gr
4
+ import concurrent.futures
5
 
6
+ # Set device
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
8
 
9
+ # Load models and tokenizers
10
+ def load_model(name):
11
+ tokenizer = AutoTokenizer.from_pretrained(name)
12
+ model = AutoModelForCausalLM.from_pretrained(name)
13
+
14
+ # Define pad token explicitly
15
+ tokenizer.pad_token = tokenizer.eos_token
16
+ model.config.pad_token_id = tokenizer.pad_token_id
17
+
18
+ return tokenizer, model.to(device)
19
 
20
+ tokenizer1, model1 = load_model("gpt2")
21
+ tokenizer2, model2 = load_model("distilgpt2")
22
+ tokenizer3, model3 = load_model("gpt2")
23
 
24
+ # Generation function
25
  def generate_response(model, tokenizer, prompt):
26
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(device)
27
+ outputs = model.generate(
28
+ inputs["input_ids"],
29
+ attention_mask=inputs["attention_mask"],
30
+ max_length=100,
31
+ pad_token_id=tokenizer.pad_token_id,
32
+ do_sample=True,
33
+ temperature=0.7,
34
+ top_p=0.9
35
+ )
36
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
37
 
38
+ # Multi-agent handler
 
39
  def multi_agent_chat(user_input):
40
+ with concurrent.futures.ThreadPoolExecutor() as executor:
41
+ futures = [
42
+ executor.submit(generate_response, model1, tokenizer1, user_input),
43
+ executor.submit(generate_response, model2, tokenizer2, user_input),
44
+ executor.submit(generate_response, model3, tokenizer3, user_input)
45
+ ]
46
+ results = [f.result() for f in futures]
47
+ return results
48
+
49
+ # Gradio Interface
50
  interface = gr.Interface(
51
  fn=multi_agent_chat,
52
  inputs=gr.Textbox(lines=2, placeholder="Ask something..."),
53
  outputs=[
54
  gr.Textbox(label="Agent 1 (GPT-2)"),
55
+ gr.Textbox(label="Agent 2 (DistilGPT-2)"),
56
+ gr.Textbox(label="Agent 3 (GPT-2)")
57
  ],
58
+ title="3-Agent AI Chatbot",
59
+ description="Three GPT-style agents respond to your input in parallel!"
60
  )
61
 
62
  interface.launch()