tharun1507 commited on
Commit
7593627
·
verified ·
1 Parent(s): 175f49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -51
app.py CHANGED
@@ -1,71 +1,34 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import time
4
 
5
- # Load the GPT-Neo model for text generation
6
- text_generator = pipeline('text-generation', model='EleutherAI/gpt-neo-125M')
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():
17
- global x
18
- x = 5
19
- foo()
20
- print(x)
21
- Expected output: 5
22
- Explanation: The `global` keyword allows `foo` to modify the variable `x`.
23
-
24
- Example 2:
25
- def modify_list(input_list):
26
- input_list.append(4)
27
- input_list = [1, 2, 3]
28
- my_list = [0]
29
- modify_list(my_list)
30
- print(my_list)
31
- Expected output: [0, 4]
32
- Explanation: The function modifies the original list by appending `4`.
33
-
34
- Example 3:
35
- print('hello')
36
- Expected output: hello
37
- Explanation: This line prints the string `hello`.
38
- """
39
-
40
  prompt = f"""
41
- Explain the following code snippet step-by-step using these examples as references:
42
- {python_code_examples}
43
-
44
  Code Snippet:
45
- ```
46
  {code_snippet}
47
  ```
48
 
49
- Provide a detailed breakdown of how the code works, including the expected output.
50
  """
51
 
52
- try:
53
- print("Generating text...")
54
- response = text_generator(prompt, max_new_tokens=100, num_return_sequences=1)
55
- print("Text generation complete.")
56
- return response[0]['generated_text'].strip()
57
- except Exception as e:
58
- return f"An error occurred: {str(e)}"
59
- finally:
60
- elapsed_time = time.time() - start_time
61
- print(f"Processing time: {elapsed_time:.2f} seconds")
62
 
63
  # Define the Gradio interface
64
  iface = gr.Interface(
65
  fn=get_completion,
66
  inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)],
67
- outputs=[gr.Textbox(label="Explanation Here", lines=8)],
68
- title="Code Explainer"
69
  )
70
 
 
71
  iface.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
+ # Load a faster text generation model (DistilGPT-2)
5
+ text_generator = pipeline('text-generation', model='distilgpt2')
6
 
7
  def get_completion(code_snippet):
8
+ # Prompt to explain the code snippet provided by the user
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  prompt = f"""
10
+ Please explain the following Python code snippet in detail, including what it does and the expected output:
11
+
 
12
  Code Snippet:
13
+ ```python
14
  {code_snippet}
15
  ```
16
 
17
+ Include any relevant information that helps clarify how the code works.
18
  """
19
 
20
+ # Generate text using the model
21
+ response = text_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.5)
22
+
23
+ return response[0]['generated_text'].strip()
 
 
 
 
 
 
24
 
25
  # Define the Gradio interface
26
  iface = gr.Interface(
27
  fn=get_completion,
28
  inputs=[gr.Textbox(label="Insert Code Snippet", lines=5)],
29
+ outputs=[gr.Textbox(label="Explanation Here", lines=10)],
30
+ title="Code Explainer Using Transformers"
31
  )
32
 
33
+ # Launch the Gradio interface
34
  iface.launch()