danrdoran commited on
Commit
bdad49d
·
verified ·
1 Parent(s): b79e86b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
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" # Change to the model you're using
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
- # Generate and display response using the LLaMA model
38
- if student_question:
39
- # Adjust prompt to encourage student-friendly responses
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=75, # Adjust this based on desired response length
46
- temperature=temperature, # Control randomness
47
- top_p=top_p, # Nucleus sampling
48
- top_k=top_k, # Top-k sampling
49
- do_sample=do_sample # Enable or disable sampling
50
  )
51
 
52
- st.write("Tutor's Answer:", response[0]['generated_text'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)