Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import random | |
| from huggingface_hub import InferenceClient | |
| # ===== LOAD & PROCESS YOUR NEW CONTENT ===== | |
| from sentence_transformers import SentenceTransformer | |
| import torch | |
| # ===== APPLY THE COMPLETE WORKFLOW ===== | |
| # Open the water_cycle.txt file in read mode with UTF-8 encoding | |
| with open("rrights.txt", "r", encoding="utf-8") as file: | |
| # Read the entire contents of the file and store it in a variable | |
| rrights_text = file.read() | |
| # Print the text below | |
| print(rrights_text) | |
| # ===== EXPERIMENT & VERIFY ===== | |
| def preprocess_text(text): | |
| # Strip extra whitespace from the beginning and the end of the text | |
| cleaned_text = text.strip() | |
| # Split the cleaned_text by every newline character (\n) | |
| chunks = cleaned_text.split(".") | |
| # Create an empty list to store cleaned chunks | |
| cleaned_chunks = [] | |
| # Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list | |
| for chunk in chunks: | |
| stripped_chunk = chunk.strip() | |
| cleaned_chunks.append(stripped_chunk) | |
| # Print cleaned_chunks | |
| print(cleaned_chunks) | |
| # Print the length of cleaned_chunks | |
| print(len(cleaned_chunks)) | |
| # Return the cleaned_chunks | |
| return cleaned_chunks | |
| # Call the preprocess_text function and store the result in a cleaned_chunks variable | |
| cleaned_chunks = preprocess_text(rrights_text) # Complete this line | |
| # Load the pre-trained embedding model that converts text to vectors | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| def create_embeddings(text_chunks): | |
| # Convert each text chunk into a vector embedding and store as a tensor | |
| chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list | |
| # Print the chunk embeddings | |
| print(chunk_embeddings) | |
| # Print the shape of chunk_embeddings | |
| print(chunk_embeddings.shape) | |
| # Return the chunk_embeddings | |
| return chunk_embeddings | |
| # Call the create_embeddings function and store the result in a new chunk_embeddings variable | |
| chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line | |
| # Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks | |
| def get_top_chunks(query, chunk_embeddings, text_chunks): | |
| # Convert the query text into a vector embedding | |
| query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line | |
| # Normalize the query embedding to unit length for accurate similarity comparison | |
| query_embedding_normalized = query_embedding / query_embedding.norm() | |
| # Normalize all chunk embeddings to unit length for consistent comparison | |
| chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) | |
| # Calculate cosine similarity between query and all chunks using matrix multiplication | |
| similarities = torch.matmul(chunk_embeddings_normalized,query_embedding_normalized) # Complete this line | |
| # Print the similarities | |
| print(similarities) | |
| # Find the indices of the 3 chunks with highest similarity scores | |
| top_indices = torch.topk(similarities, k=5).indices | |
| # Print the top indices | |
| print(top_indices) | |
| # Create an empty list to store the most relevant chunks | |
| top_chunks = [] | |
| # Loop through the top indices and retrieve the corresponding text chunks | |
| for i in top_indices: | |
| relevant_info = cleaned_chunks[i] | |
| top_chunks.append(relevant_info) | |
| # Return the list of most relevant chunks | |
| return top_chunks | |
| client = InferenceClient('Qwen/Qwen2.5-72B-Instruct') | |
| def respond(message, history): | |
| # Call the get_top_chunks function with the original query | |
| top_results = get_top_chunks(message, chunk_embeddings, cleaned_chunks) # Complete this line | |
| # Print the top results | |
| print(top_results) | |
| messages = [{"role": "system", "content": f"You are a friendly chatbot. You give people information about ways to get involved in different social causes. Base your response on the following information {top_results}"}] | |
| if history: | |
| messages.extend(history) | |
| messages.append({"role": "user", "content": message}) | |
| response = client.chat_completion(messages, max_tokens = 500) | |
| return response['choices'][0]['message']['content'].strip() | |
| #chatbot = gr.ChatInterface(respond, type="messages") | |
| #gr.Markdown("P2A", elem_id="title") | |
| #gr.Image("chatbot_logo.png", width=100) | |
| spotify_embed_code = """<iframe data-testid="embed-iframe" style="border-radius:12px" src="https://open.spotify.com/embed/playlist/7eWlClzmwXPwvAjxPJYn7Q?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe> | |
| """ | |
| theme = gr.themes.Base( | |
| primary_hue="pink", | |
| secondary_hue="indigo", | |
| neutral_hue="rose", | |
| font=['Montserrat', 'ui-sans-serif', 'system-ui', 'sans-serif'], | |
| ).set( | |
| background_fill_primary='*neutral_50', | |
| shadow_drop='0 1px 4px 0 rgb(0 0 0 / 0.1)', | |
| shadow_drop_lg='0 2px 5px 0 rgb(0 0 0 / 0.1)', | |
| shadow_spread='6px', | |
| block_background_fill='white', | |
| block_border_width='0px', | |
| block_border_width_dark='0px', | |
| block_label_background_fill='*primary_100', | |
| block_label_background_fill_dark='*primary_600', | |
| block_label_text_color='*primary_500', | |
| block_label_text_color_dark='white', | |
| block_label_margin='*spacing_md', | |
| block_label_padding='*spacing_sm *spacing_md', | |
| block_label_radius='*radius_md', | |
| block_label_text_size='*text_md', | |
| block_label_text_weight='600', | |
| block_title_background_fill='*block_label_background_fill', | |
| block_title_background_fill_dark='*block_label_background_fill', | |
| block_title_text_color='*primary_500', | |
| block_title_text_color_dark='white', | |
| block_title_padding='*block_label_padding', | |
| block_title_radius='*block_label_radius', | |
| block_title_text_weight='600', | |
| panel_border_width='1px', | |
| panel_border_width_dark='1px', | |
| checkbox_background_color_selected='*primary_600', | |
| checkbox_background_color_selected_dark='*primary_700', | |
| checkbox_border_color='*neutral_100', | |
| checkbox_border_color_dark='*neutral_600', | |
| checkbox_border_color_focus='*primary_500', | |
| checkbox_border_color_focus_dark='*primary_600', | |
| checkbox_border_color_selected='*primary_600', | |
| checkbox_border_color_selected_dark='*primary_700', | |
| checkbox_border_width='1px', | |
| checkbox_shadow='none', | |
| checkbox_label_background_fill_selected='*primary_500', | |
| checkbox_label_background_fill_selected_dark='*primary_600', | |
| checkbox_label_shadow='*shadow_drop_lg', | |
| checkbox_label_text_color_selected='white', | |
| input_background_fill='white', | |
| input_border_color='*neutral_50', | |
| input_shadow='*shadow_drop', | |
| input_shadow_dark='*shadow_drop', | |
| input_shadow_focus='*shadow_drop_lg', | |
| input_shadow_focus_dark='*shadow_drop_lg', | |
| slider_color='*primary_500', | |
| slider_color_dark='*primary_600', | |
| button_primary_background_fill_hover='*primary_400', | |
| button_primary_background_fill_hover_dark='*primary_500', | |
| button_primary_shadow='*shadow_drop_lg', | |
| button_primary_shadow_hover='*shadow_drop_lg', | |
| button_primary_shadow_active='*shadow_inset', | |
| button_primary_shadow_dark='*shadow_drop_lg', | |
| button_secondary_background_fill='white', | |
| button_secondary_background_fill_hover='*neutral_100', | |
| button_secondary_background_fill_hover_dark='*primary_500', | |
| button_secondary_text_color='*neutral_800', | |
| button_secondary_shadow='*shadow_drop_lg', | |
| button_secondary_shadow_hover='*shadow_drop_lg', | |
| button_secondary_shadow_active='*shadow_inset', | |
| button_secondary_shadow_dark='*shadow_drop_lg' | |
| ) | |
| with gr.Blocks(theme=theme) as demo: | |
| #gr.ChatInterface(respond, type="messages") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Image(value = "pta.jpg", show_label = False, show_share_button = False, show_download_button = False) | |
| title = "Passion to Action" | |
| topics = """ | |
| We are dedicated to helping you pursue causes you care about! Ask us about any of the following, and we will give you ways to get involved! | |
| Ask me any of the following questions to get involved: | |
| "I am passionate about LGBTQIA+ rights. What can I do to get involved?" | |
| "I care deeply about reproductive rights. Are there places I can donate?" | |
| "I want to help end educational funding disparities. Where can I start?" | |
| "I care very much about immigration rights. What can I do to help?" | |
| """ | |
| with gr.TabItem("Passion to Action"): | |
| with gr.Column(): | |
| gr.Markdown(topics) #saying topics not defined | |
| gr.ChatInterface( | |
| fn = respond, | |
| type = "messages", | |
| examples = [ | |
| "I am passionate about LGBTQIA+ rights. What can I do to get involved?", | |
| "I care deeply about reproductive rights. Are there places I can donate?", | |
| "I want to help end educational funding disparities. Where can I start?", | |
| "I care very much about immigration rights. What can I do to help?", | |
| ] | |
| ) | |
| with gr.Row(scale = 1): | |
| gr.Markdown("### Enjoy Passion 2 Action's favorite songs!") | |
| with gr.Row(scale = 1): | |
| gr.HTML(spotify_embed_code) | |
| demo.launch() | |