tharun1507 commited on
Commit
b7e6688
·
verified ·
1 Parent(s): 25822d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -51
app.py CHANGED
@@ -2,15 +2,15 @@ import gradio as gr
2
  from transformers import pipeline
3
  import time
4
 
5
- # Load the text generation model (GPT-2)
6
- text_generator = pipeline('text-generation', model='gpt2')
7
 
8
  def get_completion(code_snippet):
9
  print("Received code snippet, starting processing...")
10
  start_time = time.time()
11
-
 
12
  python_code_examples = """
13
- ---------------------
14
  Example 1:
15
  x = 10
16
  def foo():
@@ -18,68 +18,30 @@ def get_completion(code_snippet):
18
  x = 5
19
  foo()
20
  print(x)
21
- Correct output: 5
22
- Explanation: The global keyword allows foo() to modify the global variable x, so print(x) outputs 5.
23
 
24
- ---------------------
25
  Example 2:
26
- def modify_list(input_list):
27
- input_list.append(4)
28
- input_list = [1, 2, 3]
29
- my_list = [0]
30
- modify_list(my_list)
31
- print(my_list)
32
- Correct output: [0, 4]
33
- Explanation: The function appends 4 to input_list, which modifies my_list directly. The reassignment does not affect the original list.
34
-
35
- ---------------------
36
- Example 3:
37
  print('hello')
38
- Correct output: hello
39
- Explanation: This line outputs the string 'hello' to the console.
40
- ---------------------
41
- Example 4:
42
- for i in range(5):
43
- print(i)
44
- Correct output:
45
- 0
46
- 1
47
- 2
48
- 3
49
- 4
50
- Explanation: This loop iterates from 0 to 4, printing each number on a new line.
51
- ---------------------
52
- Example 5:
53
- def add(a, b):
54
- return a + b
55
- result = add(3, 4)
56
- print(result)
57
- Correct output: 7
58
- Explanation: The function add takes two arguments, adds them together, and returns the result. print(result) outputs 7.
59
- ---------------------
60
- Example 6:
61
- if True:
62
- print("This is true")
63
- Correct output: This is true
64
- Explanation: The condition True always evaluates to True, so the print statement is executed.
65
- ---------------------
66
  """
67
-
68
  prompt = f"""
69
- Your task is to explain the following code snippet step-by-step.
70
- Use the following examples as references for the explanations:
71
  {python_code_examples}
72
  Code Snippet:
73
  ```
74
  {code_snippet}
75
  ```
 
76
  """
77
 
78
  try:
79
  print("Generating text...")
80
- response = text_generator(prompt, max_new_tokens=100, num_return_sequences=1)
81
  print("Text generation complete.")
82
- return response[0]['generated_text']
83
  except Exception as e:
84
  return f"An error occurred: {str(e)}"
85
  finally:
 
2
  from transformers import pipeline
3
  import time
4
 
5
+ # Load the smaller and faster text generation model (DistilGPT-2)
6
+ text_generator = pipeline('text-generation', model='distilgpt2')
7
 
8
  def get_completion(code_snippet):
9
  print("Received code snippet, starting processing...")
10
  start_time = time.time()
11
+
12
+ # Provide simplified examples
13
  python_code_examples = """
 
14
  Example 1:
15
  x = 10
16
  def foo():
 
18
  x = 5
19
  foo()
20
  print(x)
21
+ Expected output: 5
22
+ Explanation: `global` allows `foo` to modify `x`.
23
 
 
24
  Example 2:
 
 
 
 
 
 
 
 
 
 
 
25
  print('hello')
26
+ Expected output: hello
27
+ Explanation: This line prints `hello`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  """
29
+
30
  prompt = f"""
31
+ Explain the following code snippet step-by-step using these examples as references:
 
32
  {python_code_examples}
33
  Code Snippet:
34
  ```
35
  {code_snippet}
36
  ```
37
+ Provide a detailed breakdown and expected output.
38
  """
39
 
40
  try:
41
  print("Generating text...")
42
+ response = text_generator(prompt, max_new_tokens=50, num_return_sequences=1)
43
  print("Text generation complete.")
44
+ return response[0]['generated_text'].strip()
45
  except Exception as e:
46
  return f"An error occurred: {str(e)}"
47
  finally: