import gradio as gr from huggingface_hub import InferenceClient from sentence_transformers import SentenceTransformer import torch import numpy as np with open("knowledge.txt" , "r", encoding="utf-8") as f: knowledge_base = f.read() print("Knowledge base loaded.") cleaned_text = knowledge_base.strip() chunks = cleaned_text.split("\n") cleaned_chunks = [] for chunk in chunks: stripped_chunk = chunk.strip() if stripped_chunk: cleaned_chunks.append(stripped_chunk) print(cleaned_chunks) model = SentenceTransformer('all-MiniLM-L6-v2') chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True) print(chunk_embeddings) def get_top_chunks(query): query_embedding = model.encode(query, convert_to_tensor=True) query_embedding_normalized = query_embedding / query_embedding.norm() chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) print(similarities) top_indices = torch.topk(similarities, k=3).indices print(top_indices) top_chunks = [] for i in top_indices: chunk = chunks[i] top_chunks.append(chunk) return top_chunks client = InferenceClient("google/gemma-3-27b-it") #def show_obgyn_info(location): #clinics = { # "New York": "NY Clinic A, NY Clinic B, NY Clinic C", # "California": "CA Clinic X, CA Clinic Y, CA Clinic Z", # "Texas": "TX Clinic 1, TX Clinic 2, TX Clinic 3", # "Florida": "FL Clinic Alpha, FL Clinic Beta, FL Clinic Gamma" # } # return clinics.get(location, "Sorry, no clinics found for this location.") #delete if doesnt work, supposed to define fn function #def respond(message,history): # if "nearest obgyn" in message.lower(): # Return a custom response (for UI to act on) # return "Sure! Please click on the 'OBGYN Finder' tab above to get started. 🩺", history #obgyn command, delete if it doesn't work messages = [{"role": "system" , "content": "Your name is BloomBot and you're a supportive and helpful chatbot catered towards teens ages 10-18. You give clear kid-appropiate explainations and keep your explainations to 10 sentences maximum." }] if history: messages.extend(history) messages.append({"role" : "user", "content" : message}) response = "" for message in client.chat_completion( messages, max_tokens = 500, stream=True, #temperature = .2 top_p = .2 ): token = message.choices[0].delta.content response += token yield response print(response) theme = gr.themes.Ocean( primary_hue="pink", secondary_hue="pink", neutral_hue="fuchsia" ) def display_image(): return "BannerBot.jpg" with gr.Blocks (theme=theme) as chatbot: gr.Image(display_image()) gr.ChatInterface(respond, type = "messages", #theme = gr.themes.Ocean( #primary_hue="pink", #secondary_hue="pink", #neutral_hue="fuchsia"), title = "Hi, I'm BloomBot!", examples = ["What are the different types of period products? ", "What are some vitamins that are good for teenage girls?", "What should I know about puberty?", "Where can I find my nearest OBGYN?"] ) with gr.Tab("OBGYN Finder") as obgyn_tab: gr.Markdown("### Find a Nearby OBGYN") location_dropdown = gr.Dropdown( choices=["New York", "California", "Texas", "Florida"], label="Choose your state" ) result_text = gr.Textbox(label="Nearby Clinics") # location_dropdown.change(fn=show_obgyn_info, inputs=location_dropdown, outputs=result_text) #obgyn finder tab # with gr.Tab("Resources"): # gr.Markdown("### Resources") # gr.HTML(""" # # # # """) #with gr.Tab("Educational PDFs"): # gr.Markdown("### 📘 Helpful Resources") chatbot.launch(debug=True)