Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,127 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
import torch
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
-
|
| 6 |
-
#hello -> this is irede
|
| 7 |
-
|
| 8 |
-
client = InferenceClient("microsoft/phi-4")
|
| 9 |
-
|
| 10 |
-
#Loading the bio spec txt file
|
| 11 |
-
|
| 12 |
-
with open("bio_spec.txt", "r", encoding="utf-8", errors="replace") as f:
|
| 13 |
-
bio_spec_text = f.read()
|
| 14 |
-
|
| 15 |
-
#process file function
|
| 16 |
-
def preprocess_text(text):
|
| 17 |
-
cleaned_text = text.strip()
|
| 18 |
-
chunks = cleaned_text.split("\n")
|
| 19 |
-
cleaned_chunks = []
|
| 20 |
-
|
| 21 |
-
for chunk in chunks:
|
| 22 |
-
chunk = chunk.strip()
|
| 23 |
-
if chunk != "":
|
| 24 |
-
cleaned_chunks.append(chunk)
|
| 25 |
-
return cleaned_chunks
|
| 26 |
-
|
| 27 |
-
#Splitting the file
|
| 28 |
-
bio_chunks = preprocess_text(bio_spec_text)
|
| 29 |
-
|
| 30 |
-
#Loading sentance transformer model and then embedding the chunks (idrk it was on colab)
|
| 31 |
-
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 32 |
-
|
| 33 |
-
chunk_embeddings = embedding_model.encode(bio_chunks, convert_to_tensor=True)
|
| 34 |
-
|
| 35 |
-
#Query embedding (on colab step 5)
|
| 36 |
-
|
| 37 |
-
def get_top_chunks(query, chunk_embeddings, text_chunks, top_k=3):
|
| 38 |
-
query_embedding = embedding_model.encode(query, convert_to_tensor=True)
|
| 39 |
-
|
| 40 |
-
query_norm = torch.nn.functional.normalize(query_embedding, p=2, dim=0)
|
| 41 |
-
chunks_norm = torch.nn.functional.normalize(chunk_embeddings, p=2, dim=1)
|
| 42 |
-
|
| 43 |
-
similarities = torch.matmul(chunks_norm, query_norm)
|
| 44 |
-
|
| 45 |
-
top_indices = torch.topk(similarities, k=top_k).indices
|
| 46 |
-
|
| 47 |
-
return [text_chunks[i] for i in top_indices]
|
| 48 |
-
|
| 49 |
-
def set_topic(topic):
|
| 50 |
-
global chosen_topic
|
| 51 |
-
chosen_topic = topic
|
| 52 |
-
return f"✅ Great! You've chosen **{topic}**. Let's start your study session."
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def respond(message, history):
|
| 57 |
-
global chosen_topic
|
| 58 |
-
|
| 59 |
-
#Getting the relevnt parts from the txt file
|
| 60 |
-
relevant_chunks = get_top_chunks(message, chunk_embeddings, bio_chunks, top_k=4)
|
| 61 |
-
spec_content = "\n".join(relevant_chunks)
|
| 62 |
-
|
| 63 |
-
system_prompt = (
|
| 64 |
-
f"You are a helpful science tutor who primarily teaches 14 to 16-year-old students "
|
| 65 |
-
f"under the UK education system, preparing them for GCSEs within the next two years. "
|
| 66 |
-
f"You are tutoring AQA GCSE Biology at both higher and foundation levels. "
|
| 67 |
-
f"Do not include content beyond this scope. "
|
| 68 |
-
f"You will be teaching them about {chosen_topic}. "
|
| 69 |
-
f"First, provide the user with information on the topic in small, digestible sections, "
|
| 70 |
-
f"preferably with each section as separate text. Always keep the aim of teaching this topic in mind. "
|
| 71 |
-
f"Once all the information on that specific topic has been covered, "
|
| 72 |
-
f"ask the user if they have any questions. If they do, answer in a way that helps them understand better. "
|
| 73 |
-
f"When the user has no more questions, give them a set of exam-style questions, one by one, "
|
| 74 |
-
f"covering different areas of the topic. "
|
| 75 |
-
f"The user may also request to focus on a specific area of the topic at first. "
|
| 76 |
-
f"After the user answers each question, provide feedback to ensure they are exam ready before moving on. "
|
| 77 |
-
f"This cycle repeats: content in small sections, check understanding, questions one by one, mark one by one, then repeat. "
|
| 78 |
-
f"Use the following specification excerpts to answer:\n{spec_content}"
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 83 |
-
|
| 84 |
-
if history:
|
| 85 |
-
messages.extend(history)
|
| 86 |
-
messages.append({"role": "user", "content": message})
|
| 87 |
-
|
| 88 |
-
response = client.chat_completion(
|
| 89 |
-
messages,
|
| 90 |
-
max_tokens=300
|
| 91 |
-
)
|
| 92 |
-
return response['choices'][0]['message']['content'].strip()
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# Topic list
|
| 97 |
-
BIO_TOPICS = [
|
| 98 |
-
"Cell Biology",
|
| 99 |
-
"Organisation",
|
| 100 |
-
"Infection and Response",
|
| 101 |
-
"Bioenergetics",
|
| 102 |
-
"Homeostasis and Response",
|
| 103 |
-
"Inheritance, Variation and Evolution",
|
| 104 |
-
"Ecology"
|
| 105 |
-
]
|
| 106 |
-
|
| 107 |
-
chosen_topic = None
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Create the Gradio interface
|
| 111 |
-
with gr.Blocks() as demo:
|
| 112 |
-
gr.Markdown("# ACE it! 📚 — GCSE Biology Tutor")
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
topic_dropdown = gr.Dropdown(choices=BIO_TOPICS, label="Choose a Biology Topic")
|
| 116 |
-
topic_button = gr.Button("Confirm Topic")
|
| 117 |
-
|
| 118 |
-
topic_output = gr.Markdown()
|
| 119 |
-
|
| 120 |
-
chatbot = gr.ChatInterface(respond, type="messages", title="ACE it!")
|
| 121 |
-
|
| 122 |
-
topic_button.click(set_topic, inputs=topic_dropdown, outputs=topic_output)
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
demo.launch()
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|