SameerArz commited on
Commit
d8788c3
·
verified ·
1 Parent(s): f7c50b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -14,14 +14,15 @@ if not GROQ_API_KEY:
14
  # Initialize Groq client
15
  client = Groq(api_key=GROQ_API_KEY)
16
 
17
- # Set up device and load Stable Diffusion model
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
19
  pipe = StableDiffusionPipeline.from_pretrained(
20
- "sd-legacy/stable-diffusion-v1-5",
21
- torch_dtype=torch.float16,
22
- safety_checker=None, # Optional: disable NSFW filter (comment out if unwanted)
23
  ).to(device)
24
- pipe.enable_model_cpu_offload() # Optimize memory usage
25
 
26
  # Function to generate tutor output (lesson, question, feedback)
27
  def generate_tutor_output(subject, difficulty, student_input):
@@ -50,7 +51,7 @@ def generate_tutor_output(subject, difficulty, student_input):
50
  )
51
  return completion.choices[0].message.content
52
 
53
- # Function to generate images using sd-legacy/stable-diffusion-v1-5
54
  def generate_images(text, selected_model):
55
  if selected_model == "Stable Diffusion (Realism)":
56
  prompt_prefix = "realistic, detailed, vivid colors, "
@@ -62,7 +63,7 @@ def generate_images(text, selected_model):
62
  results = []
63
  for i in range(3):
64
  modified_text = f"{prompt_prefix}{text} variation {i+1}, high quality"
65
- image = pipe(modified_text, num_inference_steps=25).images[0]
66
  results.append(image)
67
  return results
68
 
@@ -110,11 +111,11 @@ with gr.Blocks(title="AI Tutor with Visuals") as demo:
110
 
111
  gr.Markdown("""
112
  ### How to Use
113
- 1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question' to get your personalized lesson, question, and feedback.
114
- 2. **Visual Section**: Select an image style and click 'Generate Visuals' to see 3 variations using Stable Diffusion v1.5 (free & open-source).
115
- 3. Review the AI-generated content to enhance your learning!
116
 
117
- *Note*: Powered by sd-legacy/stable-diffusion-v1-5. GPU recommended for faster results.
118
  """)
119
 
120
  def process_output_text(subject, difficulty, student_input):
 
14
  # Initialize Groq client
15
  client = Groq(api_key=GROQ_API_KEY)
16
 
17
+ # Set up device and load the exact model you specified
18
  device = "cuda" if torch.cuda.is_available() else "cpu"
19
+ model_id = "sd-legacy/stable-diffusion-v1-5" # Your exact model ID
20
  pipe = StableDiffusionPipeline.from_pretrained(
21
+ model_id,
22
+ torch_dtype=torch.float16, # Matches your snippet
23
+ safety_checker=None, # Optional: disabled for flexibility
24
  ).to(device)
25
+ pipe.enable_model_cpu_offload() # Optimize memory for Spaces
26
 
27
  # Function to generate tutor output (lesson, question, feedback)
28
  def generate_tutor_output(subject, difficulty, student_input):
 
51
  )
52
  return completion.choices[0].message.content
53
 
54
+ # Function to generate images using your model ID
55
  def generate_images(text, selected_model):
56
  if selected_model == "Stable Diffusion (Realism)":
57
  prompt_prefix = "realistic, detailed, vivid colors, "
 
63
  results = []
64
  for i in range(3):
65
  modified_text = f"{prompt_prefix}{text} variation {i+1}, high quality"
66
+ image = pipe(modified_text, num_inference_steps=25).images[0] # Using your model
67
  results.append(image)
68
  return results
69
 
 
111
 
112
  gr.Markdown("""
113
  ### How to Use
114
+ 1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question'.
115
+ 2. **Visual Section**: Select a style and click 'Generate Visuals' to see 3 images from sd-legacy/stable-diffusion-v1-5.
116
+ 3. Review the AI-generated content!
117
 
118
+ *Example*: Try "a photo of an astronaut riding a horse on mars" for visuals.
119
  """)
120
 
121
  def process_output_text(subject, difficulty, student_input):