shukdevdatta123 commited on
Commit
24f55b5
·
verified ·
1 Parent(s): f4c0cb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -7
app.py CHANGED
@@ -2,8 +2,9 @@ import gradio as gr
2
  import os
3
  from openai import OpenAI
4
  import time
 
5
 
6
- def solve_competitive_problem(problem_statement, language_choice, api_key):
7
  """
8
  Generate a solution for a competitive programming problem
9
 
@@ -11,6 +12,7 @@ def solve_competitive_problem(problem_statement, language_choice, api_key):
11
  problem_statement (str): The problem statement
12
  language_choice (str): Programming language for the solution
13
  api_key (str): OpenRouter API key
 
14
 
15
  Returns:
16
  str: Step-by-step solution with code
@@ -22,13 +24,17 @@ def solve_competitive_problem(problem_statement, language_choice, api_key):
22
  return "Error: Please provide a problem statement."
23
 
24
  try:
 
 
25
  # Initialize OpenAI client with OpenRouter base URL
26
  client = OpenAI(
27
  base_url="https://openrouter.ai/api/v1",
28
  api_key=api_key,
29
  )
30
 
31
- # Create prompt with language preference
 
 
32
  prompt = f"""
33
  You are an expert competitive programmer. Analyze the following problem and provide a step-by-step solution with explanations and code in {language_choice}.
34
 
@@ -36,13 +42,43 @@ Problem:
36
  {problem_statement}
37
 
38
  Your response should include:
39
- 1. Problem Analysis: Break down the problem statement and identify the key challenges
40
- 2. Approach: Describe your approach to solve the problem and why it's optimal
41
- 3. Algorithm: Explain the algorithm you'll use with time and space complexity analysis
42
- 4. Implementation: Write clean, efficient, and well-commented {language_choice} code
43
- 5. Testing: Explain how to test the solution with example inputs/outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  """
45
 
 
 
46
  # Call the model
47
  completion = client.chat.completions.create(
48
  extra_headers={
@@ -60,7 +96,11 @@ Your response should include:
60
  stream=False
61
  )
62
 
 
 
63
  solution = completion.choices[0].message.content
 
 
64
  return solution
65
 
66
  except Exception as e:
@@ -121,6 +161,15 @@ with gr.Blocks(title="Competitive Programming Assistant", theme=gr.themes.Soft()
121
  - Include the entire problem, including input/output formats and constraints
122
  - Make sure to include example inputs and outputs
123
  - For complex problems, consider adding clarifying notes
 
 
 
 
 
 
 
 
 
124
  """)
125
 
126
  # Handle form submission
 
2
  import os
3
  from openai import OpenAI
4
  import time
5
+ import markdown
6
 
7
+ def solve_competitive_problem(problem_statement, language_choice, api_key, progress=gr.Progress()):
8
  """
9
  Generate a solution for a competitive programming problem
10
 
 
12
  problem_statement (str): The problem statement
13
  language_choice (str): Programming language for the solution
14
  api_key (str): OpenRouter API key
15
+ progress: Gradio progress tracker
16
 
17
  Returns:
18
  str: Step-by-step solution with code
 
24
  return "Error: Please provide a problem statement."
25
 
26
  try:
27
+ progress(0.1, "Initializing...")
28
+
29
  # Initialize OpenAI client with OpenRouter base URL
30
  client = OpenAI(
31
  base_url="https://openrouter.ai/api/v1",
32
  api_key=api_key,
33
  )
34
 
35
+ progress(0.3, "Creating prompt...")
36
+
37
+ # Create a more detailed prompt with language preference
38
  prompt = f"""
39
  You are an expert competitive programmer. Analyze the following problem and provide a step-by-step solution with explanations and code in {language_choice}.
40
 
 
42
  {problem_statement}
43
 
44
  Your response should include:
45
+
46
+ 1. Problem Analysis:
47
+ - Clear restatement of the problem in your own words
48
+ - Identification of input/output formats
49
+ - Key constraints and edge cases to consider
50
+ - Time and space complexity requirements
51
+
52
+ 2. Approach:
53
+ - High-level strategy to solve the problem
54
+ - Why this approach is optimal compared to alternatives
55
+ - Any mathematical insights or observations
56
+ - Data structures that will be helpful
57
+
58
+ 3. Algorithm:
59
+ - Detailed step-by-step breakdown of the algorithm
60
+ - Clear explanation of the logic behind each step
61
+ - Time complexity analysis with justification
62
+ - Space complexity analysis with justification
63
+ - Any optimizations made to improve performance
64
+
65
+ 4. Implementation:
66
+ - Clean, efficient, and well-commented {language_choice} code
67
+ - Proper variable naming and code organization
68
+ - Error handling and edge case management
69
+ - Optimized for both readability and performance
70
+
71
+ 5. Testing:
72
+ - Example test cases with expected outputs
73
+ - Edge case testing scenarios
74
+ - Explanation of how to verify correctness
75
+ - Potential areas where the solution might need improvement
76
+
77
+ Format your answer with clear headings and subheadings. Use markdown formatting for better readability.
78
  """
79
 
80
+ progress(0.5, "Generating solution...")
81
+
82
  # Call the model
83
  completion = client.chat.completions.create(
84
  extra_headers={
 
96
  stream=False
97
  )
98
 
99
+ progress(0.9, "Processing response...")
100
+
101
  solution = completion.choices[0].message.content
102
+
103
+ progress(1.0, "Complete!")
104
  return solution
105
 
106
  except Exception as e:
 
161
  - Include the entire problem, including input/output formats and constraints
162
  - Make sure to include example inputs and outputs
163
  - For complex problems, consider adding clarifying notes
164
+
165
+ ### Solution Format:
166
+
167
+ Your solution will include:
168
+ 1. **Problem Analysis** - Breaking down the problem into manageable components
169
+ 2. **Approach** - Strategic methodology with mathematical insights
170
+ 3. **Algorithm** - Detailed step-by-step procedure with complexity analysis
171
+ 4. **Implementation** - Clean, commented code in your chosen language
172
+ 5. **Testing** - Example test cases and verification methods
173
  """)
174
 
175
  # Handle form submission