Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ hf_token = os.getenv("TUTOR_LLAMA")
|
|
8 |
login(token=hf_token)
|
9 |
|
10 |
# Load LLaMA model and tokenizer for Arabic and ESL tutoring
|
11 |
-
model_name = "meta-llama/Llama-3.2-1B" #
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
14 |
|
@@ -34,19 +34,31 @@ do_sample = st.sidebar.checkbox("Enable Random Sampling", value=True) # Enable
|
|
34 |
# Input field for the student
|
35 |
student_question = st.text_input("Ask your question in English or Arabic!")
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
-
#
|
40 |
-
prompt = f"Please explain the answer step by step in simple terms to a young student: '{student_question}'"
|
41 |
-
|
42 |
-
# Call the pipeline with adjusted parameters
|
43 |
response = model_pipeline(
|
44 |
prompt,
|
45 |
-
max_length=
|
46 |
-
temperature=temperature,
|
47 |
-
top_p=top_p,
|
48 |
-
top_k=top_k,
|
49 |
-
do_sample=do_sample
|
50 |
)
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
login(token=hf_token)
|
9 |
|
10 |
# Load LLaMA model and tokenizer for Arabic and ESL tutoring
|
11 |
+
model_name = "meta-llama/Llama-3.2-1B" # Adjust to the LLaMA model you're using
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
14 |
|
|
|
34 |
# Input field for the student
|
35 |
student_question = st.text_input("Ask your question in English or Arabic!")
|
36 |
|
37 |
+
# Function to generate response with post-processing
|
38 |
+
def generate_response(prompt, max_length=75):
|
39 |
+
# Generate the model's response
|
|
|
|
|
|
|
40 |
response = model_pipeline(
|
41 |
prompt,
|
42 |
+
max_length=max_length,
|
43 |
+
temperature=temperature,
|
44 |
+
top_p=top_p,
|
45 |
+
top_k=top_k,
|
46 |
+
do_sample=do_sample
|
47 |
)
|
48 |
|
49 |
+
# Extract the generated text and remove the prompt (if necessary)
|
50 |
+
generated_text = response[0]['generated_text']
|
51 |
+
|
52 |
+
# Find the first instance of the actual generated answer (post-prompt)
|
53 |
+
cleaned_text = generated_text.replace(prompt, "").strip()
|
54 |
+
return cleaned_text
|
55 |
+
|
56 |
+
# Generate and display response using the LLaMA model
|
57 |
+
if student_question:
|
58 |
+
# Format the prompt to guide the model to respond conversationally and concisely
|
59 |
+
prompt = f"Q: {student_question}\nA: Explain it simply to a young student in no more than 3 sentences."
|
60 |
+
|
61 |
+
# Call the function to generate and clean the response
|
62 |
+
answer = generate_response(prompt)
|
63 |
+
|
64 |
+
st.write("Tutor's Answer:", answer)
|