CapstoneProject / app.py
mingbaer's picture
Update app.py
917b532 verified
# FRONTEND: Python library that makes it super easy to build simple user interfaces (UIs)
import gradio as gr
# BACKEND: tool from Hugging Face library to send messages to AI models and get answers back
from huggingface_hub import InferenceClient
# Helpful commentary from ChatGPT:
# Gradio is the face and mouth — it lets people talk to the robot.
# InferenceClient is the brain connector — it lets your robot talk to a super-smart brain (the Hugging Face model) and get answers.
from sentence_transformers import SentenceTransformer
# a Python library that allows you to turn sentences into numerical vector embeddings
import torch
# a machine learning library that that performs cosine similarity calculations
import numpy as np
theme = gr.themes.Soft(
primary_hue="rose",
secondary_hue="rose",
neutral_hue="rose",
).set(
link_text_color='*secondary_700',
background_fill_primary = 'primary_600'
)
# upload knowledge base - from sentiment analysis lab
with open("essay_writing.txt", "r", encoding="utf-8") as f:
essay_writing = f.read()
with open("STAAR_essay.txt", "r", encoding="utf-8") as file:
staar_essay = file.read()
# split the text into chunks
def preprocess_text(text):
cleaned_text = text.strip()
chunks = cleaned_text.split("\n")
cleaned_chunks = []
for chunk in chunks:
stripped_chunk = chunk.strip()
cleaned_chunks.append(stripped_chunk)
return cleaned_chunks
essay_chunks = preprocess_text(essay_writing)
staar_chunks = preprocess_text(staar_essay)
essay_chunks.extend(staar_chunks)
# load an embedding model
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
return chunk_embeddings
essay_embeddings = create_embeddings(essay_chunks)
def pull_relevant_info(message, chunk_embeddings, text_chunks):
query_embedding = model.encode(message, convert_to_tensor=True)
query_embedding = query_embedding / query_embedding.norm()
norm_chunk_embeddings = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
similarities = torch.matmul(norm_chunk_embeddings, query_embedding)
top_indices = torch.topk(similarities, k=3).indices
top_chunks = []
for i in top_indices:
relevant_info = text_chunks[i]
top_chunks.append(relevant_info)
return top_chunks
client = InferenceClient("microsoft/phi-4")
def respond(message, history):
info = pull_relevant_info(message, essay_embeddings, essay_chunks)
system_message = (f"You are a helpful and kind teacher named Ms. Honey. You respond clearly in no more than three complete sentences. If a user asks you to write something for them, you refuse and remind them they are capable of writing the piece themselves. Use the following information to help answer the user's question:\n\n{info}\n\n")
messages = [{"role": "system", "content": system_message}]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
for message_chunk in client.chat_completion(
messages,
max_tokens=1000,
stream=True
):
token = message_chunk['choices'][0]['delta'].get('content', '')
response += token
yield response
title = "# Writing Tutor"
topics = """
### Meet your friendly writing tutor, an AI-driven partner to turn to when you need help writing an essay.
Feel free to ask me about the topics below:
- How to organize your essay
- What a thesis is and how to write it
- How to craft an introduction paragraph
- What your body paragraphs should accomplish
- Important things to include in your conclusion
- Examples of topic sentences
"""
title_exemplar = "# Need help? Read an exemplar essay to see how it's done."
exemplar_intro = """In his speech delivered in 2013 at the dedication of Rosa Park’s statue, President Barack Obama acknowledges everything that Parks’ activism made possible in the United States. Telling the story of Parks’ life and achievements, Obama highlights the fact that Parks was a regular person whose actions accomplished enormous change during the civil rights era. Through the use of diction that portrays Parks as quiet and demure, long lists that emphasize the extent of her impacts, and Biblical references, Obama suggests that all of us are capable of achieving greater good, just as Parks did."""
exemplar_bp1 = """Although it might be a surprising way to start to his dedication, Obama begins his speech by telling us who Parks was not: “Rosa Parks held no elected office. She possessed no fortune” he explains in lines 1-2. Later, when he tells the story of the bus driver who threatened to have Parks arrested when she refused to get off the bus, he explains that Parks “simply replied, ‘You may do that’” (lines 22-23). Right away, he establishes that Parks was a regular person who did not hold a seat of power. Her protest on the bus was not part of a larger plan, it was a simple response. By emphasizing that Parks was not powerful, wealthy, or loud spoken, he implies that Parks’ style of activism is an everyday practice that all of us can aspire to."""
exemplar_bp2 = """Even though Obama portrays Parks as a demure person whose protest came “simply” and naturally, he shows the importance of her activism through long lists of ripple effects. When Parks challenged her arrest, Obama explains, Martin Luther King, Jr. stood with her and “so did thousands of Montgomery, Alabama commuters” (lines 27-28). They began a boycott that included “teachers and laborers, clergy and domestics, through rain and cold and sweltering heat, day after day, week after week, month after month, walking miles if they had to…” (lines 28-31). In this section of the speech, Obama’s sentences grow longer and he uses lists to show that Parks’ small action impacted and inspired many others to fight for change. Further, listing out how many days, weeks, and months the boycott lasted shows how Parks’ single act of protest sparked a much longer push for change."""
exemplar_bp3 = """To further illustrate Parks’ impact, Obama incorporates Biblical references that emphasize the importance of “that single moment on the bus” (lines 57-58). In lines 33-35, Obama explains that Parks and the other protestors are “driven by a solemn determination to affirm their God-given dignity” and he also compares their victory to the fall the “ancient walls of Jericho” (line 43). By of including these Biblical references, Obama suggests that Parks’ action on the bus did more than correct personal or political wrongs; it also corrected moral and spiritual wrongs. Although Parks had no political power or fortune, she was able to restore a moral balance in our world."""
exemplar_conc = """Toward the end of the speech, Obama states that change happens “not mainly through the exploits of the famous and the powerful, but through the countless acts of often anonymous courage and kindness” (lines 78-81). Through carefully chosen diction that portrays her as a quiet, regular person and through lists and Biblical references that highlight the huge impacts of her action, Obama illustrates exactly this point. He wants us to see that, just like Parks, the small and meek can change the world for the better."""
with gr.Blocks (theme = theme) as chatbot:
# chatbot = gr.Chatbot())# gr.Markdown(welcome_message)
with gr.Row():
with gr.Column():
gr.Image(value="tutor_logo.png", show_label = False, show_share_button = False, show_download_button = False)
# gr.Markdown(title)
with gr.Tabs():
with gr.TabItem("Read an Exemplar"):
gr.Markdown(title_exemplar)
gr.Markdown(exemplar_intro)
gr.Markdown(exemplar_bp1)
gr.Markdown(exemplar_bp2)
gr.Markdown(exemplar_bp3)
gr.Markdown(exemplar_conc)
with gr.TabItem("Writing Tutor"):
with gr.Column():
gr.Markdown(topics)
gr.ChatInterface(
fn=respond,
type="messages",
examples = [
"What is a thesis statement and how do I write one?",
"What should I include in my introduction paragraph?",
"I finished my body paragraphs. How can I end my essay?"
],
# submit_button = gr.Button("Submit"),
)
# submit_button.click(fn=respond, inputs=message, outputs=gr.Textbox, interactive=False, lines=10)
# chatbot = gr.ChatInterface(respond, type="messages")
chatbot.launch()