SameerArz commited on
Commit
99db205
·
verified ·
1 Parent(s): 34f1a3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -93
app.py CHANGED
@@ -1,20 +1,11 @@
1
  import gradio as gr
2
  from groq import Groq
3
  import os
4
- import threading # Import threading module
5
 
6
- # Initialize Groq client with your API key
7
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
8
 
9
- # Load Text-to-Image Models
10
- model1 = gr.load("models/prithivMLmods/SD3.5-Turbo-Realism-2.0-LoRA")
11
- model2 = gr.load("models/Purz/face-projection")
12
-
13
- # Stop event for threading (image generation)
14
- stop_event = threading.Event()
15
-
16
- # Function to generate tutor output (lesson, question, feedback)
17
- def generate_tutor_output(subject, difficulty, student_input):
18
  prompt = f"""
19
  You are an expert tutor in {subject} at the {difficulty} level.
20
  The student has provided the following input: "{student_input}"
@@ -26,53 +17,31 @@ def generate_tutor_output(subject, difficulty, student_input):
26
 
27
  Format your response as a JSON object with keys: "lesson", "question", "feedback"
28
  """
29
-
30
  completion = client.chat.completions.create(
31
- messages=[{
32
- "role": "system",
33
- "content": f"You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way and giving math examples. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {difficulty} level students."
34
- }, {
35
- "role": "user",
36
- "content": prompt,
37
- }],
38
- model="mixtral-8x7b-32768", # Model for text generation
 
 
 
39
  max_tokens=1000,
40
  )
41
-
42
- return completion.choices[0].message.content
43
-
44
- # Function to generate images based on model selection
45
- def generate_images(text, selected_model):
46
- stop_event.clear()
47
 
48
- if selected_model == "Model 1 (Turbo Realism)":
49
- model = model1
50
- elif selected_model == "Model 2 (Face Projection)":
51
- model = model2
52
- else:
53
- return ["Invalid model selection."] * 3
54
-
55
- results = []
56
- for i in range(3):
57
- if stop_event.is_set():
58
- return ["Image generation stopped by user."] * 3
59
-
60
- modified_text = f"{text} variation {i+1}"
61
- result = model(modified_text)
62
- results.append(result)
63
-
64
- return results
65
 
66
- # Set up the Gradio interface
67
  with gr.Blocks() as demo:
68
- gr.Markdown("# 🎓 Your AI Tutor with Visuals & Images")
69
-
70
- # Section for generating Text-based output (lesson, question, feedback)
71
  with gr.Row():
72
  with gr.Column(scale=2):
73
- # Input fields for subject, difficulty, and student input for textual output
74
  subject = gr.Dropdown(
75
- ["Math", "Science", "History", "Literature", "Code", "AI"],
76
  label="Subject",
77
  info="Choose the subject of your lesson"
78
  )
@@ -81,71 +50,51 @@ with gr.Blocks() as demo:
81
  label="Difficulty Level",
82
  info="Select your proficiency level"
83
  )
 
 
 
 
 
 
 
 
 
 
84
  student_input = gr.Textbox(
85
  placeholder="Type your query here...",
86
  label="Your Input",
87
  info="Enter the topic you want to learn"
88
  )
89
- submit_button_text = gr.Button("Generate Lesson & Question", variant="primary")
90
 
91
  with gr.Column(scale=3):
92
- # Output fields for lesson, question, and feedback
93
  lesson_output = gr.Markdown(label="Lesson")
94
  question_output = gr.Markdown(label="Comprehension Question")
95
  feedback_output = gr.Markdown(label="Feedback")
96
 
97
- # Section for generating Visual output
98
- with gr.Row():
99
- with gr.Column(scale=2):
100
- # Input fields for text and model selection for image generation
101
- model_selector = gr.Radio(
102
- ["Model 1 (Turbo Realism)", "Model 2 (Face Projection)"],
103
- label="Select Image Generation Model",
104
- value="Model 1 (Turbo Realism)"
105
- )
106
- submit_button_visual = gr.Button("Generate Visuals", variant="primary")
107
-
108
- with gr.Column(scale=3):
109
- # Output fields for generated images
110
- output1 = gr.Image(label="Generated Image 1")
111
- output2 = gr.Image(label="Generated Image 2")
112
- output3 = gr.Image(label="Generated Image 3")
113
-
114
  gr.Markdown("""
115
  ### How to Use
116
- 1. **Text Section**: Select a subject and difficulty, type your query, and click 'Generate Lesson & Question' to get your personalized lesson, comprehension question, and feedback.
117
- 2. **Visual Section**: Select the model for image generation, then click 'Generate Visuals' to receive 3 variations of an image based on your topic.
118
- 3. Review the AI-generated content to enhance your learning experience!
 
 
 
 
119
  """)
120
 
121
- def process_output_text(subject, difficulty, student_input):
122
  try:
123
- tutor_output = generate_tutor_output(subject, difficulty, student_input)
124
- parsed = eval(tutor_output) # Convert string to dictionary
125
  return parsed["lesson"], parsed["question"], parsed["feedback"]
126
  except:
127
  return "Error parsing output", "No question available", "No feedback available"
128
 
129
- def process_output_visual(text, selected_model):
130
- try:
131
- images = generate_images(text, selected_model) # Generate images
132
- return images[0], images[1], images[2]
133
- except:
134
- return None, None, None
135
-
136
- # Generate Text-based Output
137
- submit_button_text.click(
138
- fn=process_output_text,
139
- inputs=[subject, difficulty, student_input],
140
  outputs=[lesson_output, question_output, feedback_output]
141
  )
142
-
143
- # Generate Visual Output
144
- submit_button_visual.click(
145
- fn=process_output_visual,
146
- inputs=[student_input, model_selector],
147
- outputs=[output1, output2, output3]
148
- )
149
 
150
  if __name__ == "__main__":
151
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  from groq import Groq
3
  import os
 
4
 
5
+ # Initialize Groq client
6
  client = Groq(api_key=os.environ["GROQ_API_KEY"])
7
 
8
+ def generate_tutor_output(subject, difficulty, student_input, model):
 
 
 
 
 
 
 
 
9
  prompt = f"""
10
  You are an expert tutor in {subject} at the {difficulty} level.
11
  The student has provided the following input: "{student_input}"
 
17
 
18
  Format your response as a JSON object with keys: "lesson", "question", "feedback"
19
  """
20
+
21
  completion = client.chat.completions.create(
22
+ messages=[
23
+ {
24
+ "role": "system",
25
+ "content": f"You are the world's best AI tutor, renowned for your ability to explain complex concepts in an engaging, clear, and memorable way and giving math examples. Your expertise in {subject} is unparalleled, and you're adept at tailoring your teaching to {difficulty} level students. Your goal is to not just impart knowledge, but to inspire a love for learning and critical thinking.",
26
+ },
27
+ {
28
+ "role": "user",
29
+ "content": prompt,
30
+ }
31
+ ],
32
+ model=model,
33
  max_tokens=1000,
34
  )
 
 
 
 
 
 
35
 
36
+ return completion.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
 
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("# 🎓 Your AI Tutor by Farhan")
40
+
 
41
  with gr.Row():
42
  with gr.Column(scale=2):
 
43
  subject = gr.Dropdown(
44
+ ["Math", "Science", "History", "Geography", "Economics"],
45
  label="Subject",
46
  info="Choose the subject of your lesson"
47
  )
 
50
  label="Difficulty Level",
51
  info="Select your proficiency level"
52
  )
53
+ model_select = gr.Dropdown(
54
+ [
55
+ "mixtral-8x7b-32768",
56
+ "qwen-2.5-coder-32b",
57
+ "qwen-2.5-32b"
58
+ ],
59
+ label="AI Model",
60
+ value="mixtral-8x7b-32768",
61
+ info="Select the AI model to use"
62
+ )
63
  student_input = gr.Textbox(
64
  placeholder="Type your query here...",
65
  label="Your Input",
66
  info="Enter the topic you want to learn"
67
  )
68
+ submit_button = gr.Button("Generate Lesson", variant="primary")
69
 
70
  with gr.Column(scale=3):
 
71
  lesson_output = gr.Markdown(label="Lesson")
72
  question_output = gr.Markdown(label="Comprehension Question")
73
  feedback_output = gr.Markdown(label="Feedback")
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  gr.Markdown("""
76
  ### How to Use
77
+ 1. Select a subject from the dropdown.
78
+ 2. Choose your difficulty level.
79
+ 3. Select an AI model to power your lesson.
80
+ 4. Enter the topic or question you'd like to explore.
81
+ 5. Click 'Generate Lesson' to receive a personalized lesson, question, and feedback.
82
+ 6. Review the AI-generated content to enhance your learning.
83
+ 7. Feel free to ask follow-up questions or explore new topics!
84
  """)
85
 
86
+ def process_output(output):
87
  try:
88
+ parsed = eval(output)
 
89
  return parsed["lesson"], parsed["question"], parsed["feedback"]
90
  except:
91
  return "Error parsing output", "No question available", "No feedback available"
92
 
93
+ submit_button.click(
94
+ fn=lambda s, d, i, m: process_output(generate_tutor_output(s, d, i, m)),
95
+ inputs=[subject, difficulty, student_input, model_select],
 
 
 
 
 
 
 
 
96
  outputs=[lesson_output, question_output, feedback_output]
97
  )
 
 
 
 
 
 
 
98
 
99
  if __name__ == "__main__":
100
+ demo.launch(server_name="0.0.0.0", server_port=7860)