commit app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install required libraries
|
2 |
+
!pip install transformers gradio torch
|
3 |
+
|
4 |
+
# Import necessary libraries
|
5 |
+
import gradio as gr
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
+
|
8 |
+
# Load the model and tokenizer
|
9 |
+
model_name = "meta-llama/Llama-3.2-1B-Instruct" # or "meta-llama/Llama-3.2-3B-Instruct"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
+
|
13 |
+
def answer_question(question, max_tokens=100):
|
14 |
+
"""Generate an answer to a given question about photography."""
|
15 |
+
if not question.strip():
|
16 |
+
return "Please enter a question."
|
17 |
+
|
18 |
+
inputs = tokenizer(question, return_tensors="pt")
|
19 |
+
outputs = model.generate(**inputs, max_length=max_tokens, pad_token_id=tokenizer.eos_token_id, temperature=0.7, top_p=0.9)
|
20 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
+
|
22 |
+
def generate_practice(subject, max_length=400, temperature=0.7, top_p=0.9):
|
23 |
+
"""Generate a concise photography exercise for a given subject."""
|
24 |
+
if not subject.strip():
|
25 |
+
return "Please select a photography subject."
|
26 |
+
|
27 |
+
prompt = (f"Create a concise photography exercise for {subject}. "
|
28 |
+
f"The exercise should include: "
|
29 |
+
f"1. Objective: One sentence about what students should learn. "
|
30 |
+
f"2. Materials: List essential equipment. "
|
31 |
+
f"3. Steps: Three to four concise instructions. "
|
32 |
+
f"4. Expected outcomes: One sentence on what students should achieve.")
|
33 |
+
|
34 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
35 |
+
outputs = model.generate(**inputs, pad_token_id=tokenizer.eos_token_id, max_length=max_length, temperature=temperature, top_p=top_p)
|
36 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
+
|
38 |
+
# Define the Gradio interface using Blocks
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
# Title and Description
|
41 |
+
gr.Markdown("# πΈ Photography Learning Assistant")
|
42 |
+
gr.Markdown("Welcome to the **Photography Learning Assistant**! Use the Q&A section to ask questions or generate exercises.")
|
43 |
+
|
44 |
+
# Q&A Section
|
45 |
+
gr.Markdown("### π Q&A")
|
46 |
+
question_input = gr.Textbox(label="Photography Question", placeholder="Enter a question (e.g., What is the rule of thirds?)", lines=2)
|
47 |
+
max_tokens_slider = gr.Slider(minimum=50, maximum=500, step=50, value=100, label="Max Tokens")
|
48 |
+
answer_button = gr.Button("Get Answer")
|
49 |
+
answer_output = gr.Textbox(label="Answer", lines=10)
|
50 |
+
answer_button.click(fn=answer_question, inputs=[question_input, max_tokens_slider], outputs=answer_output)
|
51 |
+
|
52 |
+
gr.Markdown("#### π‘ Sample Questions")
|
53 |
+
gr.Markdown("""
|
54 |
+
- What are different types of photography?
|
55 |
+
- Explain the exposure triangle like you would explain to a 5-year-old.
|
56 |
+
""")
|
57 |
+
|
58 |
+
|
59 |
+
# Generate Practice Exercise Section
|
60 |
+
gr.Markdown("### π― Generate Practice Exercise")
|
61 |
+
subject_dropdown = gr.Radio(choices=["Composition", "Lighting", "Camera Settings", "Exposure", "Post-Processing"], label="Photography Subject")
|
62 |
+
max_length_slider = gr.Slider(minimum=100, maximum=800, step=50, value=400, label="Max Length")
|
63 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.7, label="Temperature")
|
64 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.9, label="Top P")
|
65 |
+
generate_button = gr.Button("Generate Exercise")
|
66 |
+
practice_output = gr.Textbox(label="Generated Practice Exercise", lines=15)
|
67 |
+
generate_button.click(fn=generate_practice, inputs=[subject_dropdown, max_length_slider, temperature_slider, top_p_slider], outputs=practice_output)
|
68 |
+
|
69 |
+
# Launch the Gradio app
|
70 |
+
demo.launch(share=True)
|