Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,43 @@
|
|
1 |
-
import
|
2 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
intent = detect_intent(user_input)
|
46 |
-
logic = handle_logic(intent)
|
47 |
-
response = generate_reply(logic)
|
48 |
-
return response
|
49 |
-
|
50 |
-
# ---------------- Gradio UI ----------------
|
51 |
-
gr.Interface(
|
52 |
-
fn=chatbot,
|
53 |
-
inputs=gr.Textbox(label="User Input"),
|
54 |
-
outputs=gr.Textbox(label="Chatbot Response"),
|
55 |
-
title="3-Agent Chatbot",
|
56 |
-
description="Intent Detection → Domain Logic → Natural Language Generation"
|
57 |
-
).launch()
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
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()
|
43 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|