Spaces:
Runtime error
Runtime error
File size: 9,713 Bytes
ab82d83 8e6482c d3a8e26 8e6482c 29f636b dcae35d 29f636b 2e2648c a50a5bf 2e2648c a50a5bf 917b532 a50a5bf 917b532 26dc41c a50a5bf 26dc41c a50a5bf 26dc41c a50a5bf 917b532 a50a5bf 917b532 32de873 7d9cd2d 32de873 ca7b7b5 917b532 bc729b4 ca7b7b5 efc7bd6 ca7b7b5 603aeba ca7b7b5 7b03857 26dc41c 2aa6e7f c34066e 2aa6e7f 8e6482c 26dc41c d95850b 9321881 96bb21e 2aa6e7f 58b2b94 8ac1594 bfcb0e5 c3ec261 bfcb0e5 e81d986 3919274 26dc41c 3919274 2c1dac7 26dc41c ca7b7b5 8bd6992 a50a5bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# 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()
|