Hrishabh commited on
Commit
44a038a
1 Parent(s): 99a0f0f

Initial commit of Streamlit app

Browse files
Files changed (3) hide show
  1. README.md +14 -0
  2. moa_app.py.py +108 -0
  3. requirements.txt.txt +4 -0
README.md CHANGED
@@ -11,3 +11,17 @@ license: mit
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+
16
+ # MOA-NLP-Toolkit
17
+
18
+ ## Overview
19
+
20
+ The MOA-NLP-Toolkit is an interactive web application built with Streamlit, designed to showcase the capabilities of advanced natural language processing (NLP) using a Mixture of Agents (MoA) approach. It integrates models from the Hugging Face Transformers library to perform tasks such as text summarization, question-answering, and text generation.
21
+
22
+ ## Features
23
+
24
+ - **Summarization**: Automatically condenses long texts into concise summaries.
25
+ - **Question-Answering**: Provides accurate answers to user queries based on given contexts.
26
+ - **Text Generation**: Generates new text continuations based on user-provided prompts.
27
+
moa_app.py.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import torch # Import torch for GPU availability check
4
+ import json
5
+ # Define Agent class
6
+ class Agent:
7
+ def __init__(self, task, model_name):
8
+ self.task = task
9
+ self.model = pipeline(task, model=model_name, device=0 if torch.cuda.is_available() else -1)
10
+
11
+ def run(self, input_data):
12
+ return self.model(input_data)
13
+
14
+ # Define MoAManager class
15
+ class MoAManager:
16
+ def __init__(self):
17
+ self.agents = {}
18
+
19
+ def add_agent(self, task, model_name):
20
+ self.agents[task] = Agent(task, model_name)
21
+
22
+ def run_task(self, task, input_data):
23
+ if task in self.agents:
24
+ return self.agents[task].run(input_data)
25
+ else:
26
+ return "No agent available for this task."
27
+
28
+ # Initialize MoA Manager and add agents
29
+ moa = MoAManager()
30
+ moa.add_agent("summarization", "sshleifer/distilbart-cnn-6-6")
31
+ moa.add_agent("question-answering", "distilbert-base-uncased-distilled-squad")
32
+ moa.add_agent("text-generation", "gpt2")
33
+
34
+ # Streamlit Interface
35
+ st.title("Advanced Mixture of Agents (MoA) Demo")
36
+ st.sidebar.header("Configuration")
37
+ task = st.sidebar.selectbox("Choose a task", ["summarization", "question-answering", "text-generation"])
38
+
39
+ st.markdown("""
40
+ <style>
41
+ .big-font {
42
+ font-size:20px !important;
43
+ }
44
+ .highlight {
45
+ background-color: #f0f0f5;
46
+ padding: 10px;
47
+ border-radius: 5px;
48
+ color: black; /* Set text color to black */
49
+ }
50
+ </style>
51
+ """, unsafe_allow_html=True)
52
+
53
+ input_text = st.text_area("Input Text", height=200)
54
+
55
+ if task == "question-answering":
56
+ question = st.text_input("Question", "")
57
+
58
+ if st.button("Run Task"):
59
+ st.markdown('<p class="big-font highlight">Result:</p>', unsafe_allow_html=True)
60
+ with st.spinner("Processing..."):
61
+ if task == "summarization":
62
+ result = moa.run_task(task, input_text)
63
+ if result:
64
+ st.write(result[0]['summary_text'])
65
+ else:
66
+ st.error("Error processing summarization task.")
67
+ elif task == "question-answering":
68
+ input_data = {"question": question, "context": input_text}
69
+ result = moa.run_task(task, input_data)
70
+ formatted_result = json.dumps(result, indent=4)
71
+ st.code(formatted_result, language='json')
72
+
73
+ elif task == "text-generation":
74
+ result = moa.run_task(task, input_text)
75
+ if result:
76
+ st.write(result[0]['generated_text'])
77
+ else:
78
+ st.error("Error processing text-generation task.")
79
+
80
+ # Load data examples
81
+ st.sidebar.header("Example Data")
82
+ example_task = st.sidebar.selectbox("Choose example task", ["summarization", "question-answering", "text-generation"])
83
+ if example_task == "summarization":
84
+ st.sidebar.markdown("""
85
+ **Example Input:**
86
+ ```
87
+ 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.
88
+ ```
89
+ """)
90
+ elif example_task == "question-answering":
91
+ st.sidebar.markdown("""
92
+ **Example Context:**
93
+ ```
94
+ 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.
95
+ ```
96
+ **Example Question:**
97
+ ```
98
+ Who coined the term 'artificial intelligence' and when?
99
+ ```
100
+ """)
101
+ elif example_task == "text-generation":
102
+ st.sidebar.markdown("""
103
+ **Example Input:**
104
+ ```
105
+ Once upon a time,
106
+ ```
107
+ """)
108
+
requirements.txt.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch
4
+