File size: 4,532 Bytes
44a038a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import streamlit as st
from transformers import pipeline
import torch  # Import torch for GPU availability check
import json
# Define Agent class
class Agent:
    def __init__(self, task, model_name):
        self.task = task
        self.model = pipeline(task, model=model_name, device=0 if torch.cuda.is_available() else -1)

    def run(self, input_data):
        return self.model(input_data)

# Define MoAManager class
class MoAManager:
    def __init__(self):
        self.agents = {}

    def add_agent(self, task, model_name):
        self.agents[task] = Agent(task, model_name)

    def run_task(self, task, input_data):
        if task in self.agents:
            return self.agents[task].run(input_data)
        else:
            return "No agent available for this task."

# Initialize MoA Manager and add agents
moa = MoAManager()
moa.add_agent("summarization", "sshleifer/distilbart-cnn-6-6")
moa.add_agent("question-answering", "distilbert-base-uncased-distilled-squad")
moa.add_agent("text-generation", "gpt2")

# Streamlit Interface
st.title("Advanced Mixture of Agents (MoA) Demo")
st.sidebar.header("Configuration")
task = st.sidebar.selectbox("Choose a task", ["summarization", "question-answering", "text-generation"])

st.markdown("""
<style>
    .big-font {
        font-size:20px !important;
    }
    .highlight {
        background-color: #f0f0f5;
        padding: 10px;
        border-radius: 5px;
        color: black; /* Set text color to black */
    }
</style>
""", unsafe_allow_html=True)

input_text = st.text_area("Input Text", height=200)

if task == "question-answering":
    question = st.text_input("Question", "")

if st.button("Run Task"):
    st.markdown('<p class="big-font highlight">Result:</p>', unsafe_allow_html=True)
    with st.spinner("Processing..."):
        if task == "summarization":
            result = moa.run_task(task, input_text)
            if result:
                st.write(result[0]['summary_text'])
            else:
                st.error("Error processing summarization task.")
        elif task == "question-answering":
            input_data = {"question": question, "context": input_text}
            result = moa.run_task(task, input_data)
            formatted_result = json.dumps(result, indent=4)
            st.code(formatted_result, language='json')
            
        elif task == "text-generation":
            result = moa.run_task(task, input_text)
            if result:
                st.write(result[0]['generated_text'])
            else:
                st.error("Error processing text-generation task.")

# Load data examples
st.sidebar.header("Example Data")
example_task = st.sidebar.selectbox("Choose example task", ["summarization", "question-answering", "text-generation"])
if example_task == "summarization":
    st.sidebar.markdown("""
    **Example Input:**
    ```
    Artificial intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think like humans and mimic their actions. The term may also be applied to any machine that exhibits traits associated with a human mind such as learning and problem-solving. AI is continuously evolving to benefit many different industries. Machines are wired using a cross-disciplinary approach based on mathematics, computer science, linguistics, psychology, and more. John McCarthy, an American computer scientist, coined the term 'artificial intelligence' in 1956 at the Dartmouth Conference, where the discipline was born.
    ```
    """)
elif example_task == "question-answering":
    st.sidebar.markdown("""
    **Example Context:**
    ```
    Artificial intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think like humans and mimic their actions. The term may also be applied to any machine that exhibits traits associated with a human mind such as learning and problem-solving. AI is continuously evolving to benefit many different industries. Machines are wired using a cross-disciplinary approach based on mathematics, computer science, linguistics, psychology, and more. John McCarthy, an American computer scientist, coined the term 'artificial intelligence' in 1956 at the Dartmouth Conference, where the discipline was born.
    ```
    **Example Question:**
    ```
    Who coined the term 'artificial intelligence' and when?
    ```
    """)
elif example_task == "text-generation":
    st.sidebar.markdown("""
    **Example Input:**
    ```
    Once upon a time,
    ```
    """)