tharun1507 commited on
Commit
b391d67
·
verified ·
1 Parent(s): 76e5603

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -1,13 +1,14 @@
1
- # -*- coding: utf-8 -*-
2
-
3
  import gradio as gr
4
  from transformers import pipeline
 
5
 
6
  # Load the text generation model (GPT-2)
7
  text_generator = pipeline('text-generation', model='gpt2')
8
 
9
  def get_completion(code_snippet):
10
- # Code examples to illustrate the output
 
 
11
  python_code_examples = """
12
  ---------------------
13
  Example 1:
@@ -18,7 +19,7 @@ def get_completion(code_snippet):
18
  foo()
19
  print(x)
20
  Correct output: 5
21
- Explanation: The global keyword allows foo() to modify the global variable x.
22
 
23
  ---------------------
24
  Example 2:
@@ -29,12 +30,19 @@ def get_completion(code_snippet):
29
  modify_list(my_list)
30
  print(my_list)
31
  Correct output: [0, 4]
32
- Explanation: The function appends 4 to input_list, which modifies my_list directly.
 
 
 
 
 
 
33
  ---------------------
34
  """
35
 
36
  prompt = f"""
37
- Your task is to explain the following code snippet step-by-step, using the examples below as reference.
 
38
  {python_code_examples}
39
  Code Snippet:
40
  ```
@@ -43,11 +51,15 @@ def get_completion(code_snippet):
43
  """
44
 
45
  try:
46
- # Generate text using the Hugging Face model
47
  response = text_generator(prompt, max_length=300, num_return_sequences=1)
 
48
  return response[0]['generated_text']
49
  except Exception as e:
50
  return f"An error occurred: {str(e)}"
 
 
 
51
 
52
  # Define the Gradio interface
53
  iface = gr.Interface(
 
 
 
1
  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:
 
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:
 
30
  modify_list(my_list)
31
  print(my_list)
32
  Correct output: [0, 4]
33
+ Explanation: input_list is modified by appending 4, but 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
  """
42
 
43
  prompt = f"""
44
+ Your task is to explain the following code snippet step-by-step.
45
+ Use the following examples as references for the explanations:
46
  {python_code_examples}
47
  Code Snippet:
48
  ```
 
51
  """
52
 
53
  try:
54
+ print("Generating text...")
55
  response = text_generator(prompt, max_length=300, num_return_sequences=1)
56
+ print("Text generation complete.")
57
  return response[0]['generated_text']
58
  except Exception as e:
59
  return f"An error occurred: {str(e)}"
60
+ finally:
61
+ elapsed_time = time.time() - start_time
62
+ print(f"Processing time: {elapsed_time:.2f} seconds")
63
 
64
  # Define the Gradio interface
65
  iface = gr.Interface(