awacke1/Wikipedia.Chat.Multiplayer
1
1import 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("""15 16Scene 1: The Enchanted Castle17 18You 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.19 20Option 1: Try to make your way through the thorns.21Option 2: Look for another way in.22 23Sentiment: Feels like harsher trials after passive sleep.24 25---26 27Scene 2: The Castle's Secrets28 29If you made it past the thorns, you discover that the castle is full of hidden chambers, each containing a different trial. 30 31These trials are designed to test your limits, reveal your inner most desires, and help you understand the suffering of humankind.32 33Option 1: Enter the first chamber.34Option 2: Continue exploring the castle.35 36Sentiment: Comedy ending in marriage.37 38---39 40Scene 3: The Princess's Awakening41 42After navigating the castle's trials, you finally reach the chamber where the princess lies sleeping. 43 44You are faced with the decision of how to awaken her, knowing that your actions will determine the nature of your relationship with her.45 46Option 1: Awaken her with a gentle kiss.47Option 2: Awaken her through a more assertive act like lifting her up.48 49Sentiment: Heart forged awakening with different implications depending on context.50 51""")52 53try:54 nlp = spacy.load("en_core_web_sm")55except:56 spacy.cli.download("en_core_web_sm")57 nlp = spacy.load("en_core_web_sm")58 59wh_words = ['what', 'who', 'how', 'when', 'which']60 61def get_concepts(text):62 text = text.lower()63 doc = nlp(text)64 concepts = []65 for chunk in doc.noun_chunks:66 if chunk.text not in wh_words:67 concepts.append(chunk.text)68 return concepts69 70def get_passages(text, k=100):71 doc = nlp(text)72 passages = []73 passage_len = 074 passage = ""75 sents = list(doc.sents)76 for i in range(len(sents)):77 sen = sents[i]78 passage_len += len(sen)79 if passage_len >= k:80 passages.append(passage)81 passage = sen.text82 passage_len = len(sen)83 continue84 elif i == (len(sents) - 1):85 passage += " " + sen.text86 passages.append(passage)87 passage = ""88 passage_len = 089 continue90 passage += " " + sen.text91 return passages92 93def get_dicts_for_dpr(concepts, n_results=20, k=100):94 dicts = []95 for concept in concepts:96 wikis = wikipedia.search(concept, results=n_results)97 st.write(f"{concept} No of Wikis: {len(wikis)}")98 for wiki in wikis:99 try:100 html_page = wikipedia.page(title=wiki, auto_suggest=False)101 except DisambiguationError:102 continue103 htmlResults = html_page.content104 passages = get_passages(htmlResults, k=k)105 for passage in passages:106 i_dicts = {}107 i_dicts['text'] = passage108 i_dicts['title'] = wiki109 dicts.append(i_dicts)110 return dicts111 112passage_encoder = TFAutoModel.from_pretrained("nlpconnect/dpr-ctx_encoder_bert_uncased_L-2_H-128_A-2")113query_encoder = TFAutoModel.from_pretrained("nlpconnect/dpr-question_encoder_bert_uncased_L-2_H-128_A-2")114p_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/dpr-ctx_encoder_bert_uncased_L-2_H-128_A-2")115q_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/dpr-question_encoder_bert_uncased_L-2_H-128_A-2")116 117def get_title_text_combined(passage_dicts):118 res = []119 for p in passage_dicts:120 res.append(tuple((p['title'], p['text'])))121 return res122 123def extracted_passage_embeddings(processed_passages, max_length=156):124 passage_inputs = p_tokenizer.batch_encode_plus(125 processed_passages,126 add_special_tokens=True,127 truncation=True,128 padding="max_length",129 max_length=max_length,130 return_token_type_ids=True131 )132 passage_embeddings = passage_encoder.predict([np.array(passage_inputs['input_ids']), np.array(passage_inputs['attention_mask']), 133 np.array(passage_inputs['token_type_ids'])], 134 batch_size=64, 135 verbose=1)136 return passage_embeddings137 138def extracted_query_embeddings(queries, max_length=64):139 query_inputs = q_tokenizer.batch_encode_plus(140 queries,141 add_special_tokens=True,142 truncation=True,143 padding="max_length",144 max_length=max_length,145 return_token_type_ids=True146 )147 148 query_embeddings = query_encoder.predict([np.array(query_inputs['input_ids']),149 np.array(query_inputs['attention_mask']),150 np.array(query_inputs['token_type_ids'])],151 batch_size=1,152 verbose=1)153 return query_embeddings154 155def get_pagetext(page):156 s = str(page).replace("/t","")157 return s158 159def get_wiki_summary(search):160 wiki_wiki = wikipediaapi.Wikipedia('en')161 page = wiki_wiki.page(search) 162 163 164def get_wiki_summaryDF(search):165 wiki_wiki = wikipediaapi.Wikipedia('en')166 page = wiki_wiki.page(search)167 168 isExist = page.exists()169 if not isExist:170 return isExist, "Not found", "Not found", "Not found", "Not found"171 172 pageurl = page.fullurl173 pagetitle = page.title174 pagesummary = page.summary[0:60]175 pagetext = get_pagetext(page.text)176 177 backlinks = page.backlinks178 linklist = ""179 for link in backlinks.items():180 pui = link[0]181 linklist += pui + " , "182 a=1 183 184 categories = page.categories185 categorylist = ""186 for category in categories.items():187 pui = category[0]188 categorylist += pui + " , "189 a=1 190 191 links = page.links192 linklist2 = ""193 for link in links.items():194 pui = link[0]195 linklist2 += pui + " , "196 a=1 197 198 sections = page.sections199 200 ex_dic = {201 'Entity' : ["URL","Title","Summary", "Text", "Backlinks", "Links", "Categories"],202 'Value': [pageurl, pagetitle, pagesummary, pagetext, linklist,linklist2, categorylist ]203 }204 205 df = pd.DataFrame(ex_dic)206 207 return df208 209 210def save_message(name, message):211 now = datetime.datetime.now()212 timestamp = now.strftime("%Y-%m-%d %H:%M:%S")213 with open("chat.txt", "a") as f:214 f.write(f"{timestamp} - {name}: {message}\n")215 216def press_release():217 st.markdown("""๐๐ Breaking News! ๐ข๐ฃ218Introducing StreamlitWikipediaChat - the ultimate way to chat with Wikipedia and the whole world at the same time! ๐๐๐219Are 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! ๐๐ป220With 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. ๐ฐ๐221But 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. ๐๐จโ๐ซ๐ฉโ๐ซ222And 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! ๐คฏ๐223So, what are you waiting for? Join the fun and start chatting with Wikipedia and the world today! ๐๐224StreamlitWikipediaChat - where learning meets fun! ๐ค๐""")225 226 227def main():228 st.title("Streamlit Chat")229 230 name = st.text_input("Enter your name")231 message = st.text_input("Enter a topic to share from Wikipedia")232 if st.button("Submit"):233 234 # wiki235 df = get_wiki_summaryDF(message)236 237 save_message(name, message)238 save_message(name, df)239 240 st.text("Message sent!")241 242 243 st.text("Chat history:")244 with open("chat.txt", "a+") as f:245 f.seek(0)246 chat_history = f.read()247 #st.text(chat_history)248 st.markdown(chat_history)249 250 countdown = st.empty()251 t = 60252 while t:253 mins, secs = divmod(t, 60)254 countdown.text(f"Time remaining: {mins:02d}:{secs:02d}")255 time.sleep(1)256 t -= 1257 if t == 0:258 countdown.text("Time's up!")259 with open("chat.txt", "a+") as f:260 f.seek(0)261 chat_history = f.read()262 #st.text(chat_history)263 st.markdown(chat_history)264 265 press_release()266 267 t = 60268 269if __name__ == "__main__":270 main()271 272 