Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import time
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
text_generator = pipeline('text-generation', model='
|
| 7 |
|
| 8 |
def get_completion(code_snippet):
|
| 9 |
-
|
| 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 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
Code Snippet:
|
| 45 |
-
```
|
| 46 |
{code_snippet}
|
| 47 |
```
|
| 48 |
|
| 49 |
-
|
| 50 |
"""
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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=
|
| 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()
|