awacke1/Wikipedia.Chat.Multiplayer
1
1#In streamlit and python edit this example and add tracking of the option selections by adding buttons for the three choice sets for options. Also save the values to text file and show full history after an option is recorded. import streamlit as st2import spacy3import wikipediaapi4import wikipedia5from wikipedia.exceptions import DisambiguationError6from transformers import TFAutoModel, AutoTokenizer7import numpy as np8import pandas as pd9import faiss10import datetime11import time12 13 14st.markdown("""15Scene 1: The Enchanted Castle16You arrive at the enchanted castle, surrounded by a forest of thorns. You have heard stories of a beautiful princess asleep within, waiting for someone to awaken her.17Option 1: Try to make your way through the thorns.18Option 2: Look for another way in.19Sentiment: Feels like harsher trials after passive sleep.20---21Scene 2: The Castle's Secrets22If you made it past the thorns, you discover that the castle is full of hidden chambers, each containing a different trial. 23These trials are designed to test your limits, reveal your inner most desires, and help you understand the suffering of humankind.24Option 1: Enter the first chamber.25Option 2: Continue exploring the castle.26Sentiment: Comedy ending in marriage.27---28Scene 3: The Princess's Awakening29After navigating the castle's trials, you finally reach the chamber where the princess lies sleeping. 30You are faced with the decision of how to awaken her, knowing that your actions will determine the nature of your relationship with her.31Option 1: Awaken her with a gentle kiss.32Option 2: Awaken her through a more assertive act like lifting her up.33Sentiment: Heart forged awakening with different implications depending on context.34""")35 36try:37 nlp = spacy.load("en_core_web_sm")38except:39 spacy.cli.download("en_core_web_sm")40 nlp = spacy.load("en_core_web_sm")41 42wh_words = ['what', 'who', 'how', 'when', 'which']43 44def get_concepts(text):45 text = text.lower()46 doc = nlp(text)47 concepts = []48 for chunk in doc.noun_chunks:49 if chunk.text not in wh_words:50 concepts.append(chunk.text)51 return concepts52 53def get_passages(text, k=100):54 doc = nlp(text)55 passages = []56 passage_len = 057 passage = ""58 sents = list(doc.sents)59 for i in range(len(sents)):60 sen = sents[i]61 passage_len += len(sen)62 if passage_len >= k:63 passages.append(passage)64 passage = sen.text65 passage_len = len(sen)66 continue67 elif i == (len(sents) - 1):68 passage += " " + sen.text69 passages.append(passage)70 passage = ""71 passage_len = 072 continue73 passage += " " + sen.text74 return passages75 76def get_dicts_for_dpr(concepts, n_results=20, k=100):77 dicts = []78 for concept in concepts:79 wikis = wikipedia.search(concept, results=n_results)80 st.write(f"{concept} No of Wikis: {len(wikis)}")81 for wiki in wikis:82 try:83 html_page = wikipedia.page(title=wiki, auto_suggest=False)84 except DisambiguationError:85 continue86 htmlResults = html_page.content87 passages = get_passages(htmlResults, k=k)88 for passage in passages:89 i_dicts = {}90 i_dicts['text'] = passage91 i_dicts['title'] = wiki92 dicts.append(i_dicts)93 return dicts94 95passage_encoder = TFAutoModel.from_pretrained("nlpconnect/dpr-ctx_encoder_bert_uncased_L-2_H-128_A-2")96query_encoder = TFAutoModel.from_pretrained("nlpconnect/dpr-question_encoder_bert_uncased_L-2_H-128_A-2")97p_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/dpr-ctx_encoder_bert_uncased_L-2_H-128_A-2")98q_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/dpr-question_encoder_bert_uncased_L-2_H-128_A-2")99 100def get_title_text_combined(passage_dicts):101 res = []102 for p in passage_dicts:103 res.append(tuple((p['title'], p['text'])))104 return res105 106def extracted_passage_embeddings(processed_passages, max_length=156):107 passage_inputs = p_tokenizer.batch_encode_plus(108 processed_passages,109 add_special_tokens=True,110 truncation=True,111 padding="max_length",112 max_length=max_length,113 return_token_type_ids=True114 )115 passage_embeddings = passage_encoder.predict([np.array(passage_inputs['input_ids']), np.array(passage_inputs['attention_mask']), 116 np.array(passage_inputs['token_type_ids'])], 117 batch_size=64, 118 verbose=1)119 return passage_embeddings120 121def extracted_query_embeddings(queries, max_length=64):122 query_inputs = q_tokenizer.batch_encode_plus(123 queries,124 add_special_tokens=True,125 truncation=True,126 padding="max_length",127 max_length=max_length,128 return_token_type_ids=True129 )130 131 query_embeddings = query_encoder.predict([np.array(query_inputs['input_ids']),132 np.array(query_inputs['attention_mask']),133 np.array(query_inputs['token_type_ids'])],134 batch_size=1,135 verbose=1)136 return query_embeddings137 138def get_pagetext(page):139 s = str(page).replace("/t","")140 return s141 142def get_wiki_summary(search):143 wiki_wiki = wikipediaapi.Wikipedia('en')144 page = wiki_wiki.page(search) 145 146 147def get_wiki_summaryDF(search):148 wiki_wiki = wikipediaapi.Wikipedia('en')149 page = wiki_wiki.page(search)150 151 isExist = page.exists()152 if not isExist:153 return isExist, "Not found", "Not found", "Not found", "Not found"154 155 pageurl = page.fullurl156 pagetitle = page.title157 pagesummary = page.summary[0:60]158 pagetext = get_pagetext(page.text)159 160 backlinks = page.backlinks161 linklist = ""162 for link in backlinks.items():163 pui = link[0]164 linklist += pui + " , "165 a=1 166 167 categories = page.categories168 categorylist = ""169 for category in categories.items():170 pui = category[0]171 categorylist += pui + " , "172 a=1 173 174 links = page.links175 linklist2 = ""176 for link in links.items():177 pui = link[0]178 linklist2 += pui + " , "179 a=1 180 181 sections = page.sections182 183 ex_dic = {184 'Entity' : ["URL","Title","Summary", "Text", "Backlinks", "Links", "Categories"],185 'Value': [pageurl, pagetitle, pagesummary, pagetext, linklist,linklist2, categorylist ]186 }187 188 df = pd.DataFrame(ex_dic)189 190 return df191 192 193def save_message(name, message):194 now = datetime.datetime.now()195 timestamp = now.strftime("%Y-%m-%d %H:%M:%S")196 with open("chat.txt", "a") as f:197 f.write(f"{timestamp} - {name}: {message}\n")198 199def press_release():200 st.markdown("""๐๐ Breaking News! ๐ข๐ฃ201Introducing StreamlitWikipediaChat - the ultimate way to chat with Wikipedia and the whole world at the same time! ๐๐๐202Are you tired of reading boring articles on Wikipedia? Do you want to have some fun while learning new things? Then StreamlitWikipediaChat is just the thing for you! ๐๐ป203With StreamlitWikipediaChat, you can ask Wikipedia anything you want and get instant responses! Whether you want to know the capital of Madagascar or how to make a delicious chocolate cake, Wikipedia has got you covered. ๐ฐ๐204But that's not all! You can also chat with other people from around the world who are using StreamlitWikipediaChat at the same time. It's like a virtual classroom where you can learn from and teach others. ๐๐จโ๐ซ๐ฉโ๐ซ205And the best part? StreamlitWikipediaChat is super easy to use! All you have to do is type in your question and hit send. That's it! ๐คฏ๐206So, what are you waiting for? Join the fun and start chatting with Wikipedia and the world today! ๐๐207StreamlitWikipediaChat - where learning meets fun! ๐ค๐""")208 209 210def main():211 st.title("Streamlit Chat")212 213 name = st.text_input("Enter your name")214 message = st.text_input("Enter a topic to share from Wikipedia")215 if st.button("Submit"):216 217 # wiki218 df = get_wiki_summaryDF(message)219 220 save_message(name, message)221 save_message(name, df)222 223 st.text("Message sent!")224 225 226 st.text("Chat history:")227 with open("chat.txt", "a+") as f:228 f.seek(0)229 chat_history = f.read()230 #st.text(chat_history)231 st.markdown(chat_history)232 233 countdown = st.empty()234 t = 60235 while t:236 mins, secs = divmod(t, 60)237 countdown.text(f"Time remaining: {mins:02d}:{secs:02d}")238 time.sleep(1)239 t -= 1240 if t == 0:241 countdown.text("Time's up!")242 with open("chat.txt", "a+") as f:243 f.seek(0)244 chat_history = f.read()245 #st.text(chat_history)246 st.markdown(chat_history)247 248 press_release()249 250 t = 60251 252if __name__ == "__main__":253 main()254 