tharun1507 commited on
Commit
2f9af74
·
verified ·
1 Parent(s): e516d4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -62
app.py CHANGED
@@ -1,65 +1,59 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- # Initialize the Hugging Face Inference Client
5
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
-
7
- def explain_code(
8
- code_snippet,
9
- history: list[tuple[str, str]],
10
- system_message: str,
11
- max_tokens: int,
12
- temperature: float,
13
- top_p: float,
14
- ):
15
- # Prepare messages for the model
16
- messages = [{"role": "system", "content": system_message}]
17
-
18
- # Append previous interactions
19
- for val in history:
20
- if val[0]:
21
- messages.append({"role": "user", "content": val[0]})
22
- if val[1]:
23
- messages.append({"role": "assistant", "content": val[1]})
24
-
25
- # Add the code snippet for explanation
26
- messages.append({"role": "user", "content": f"Please provide a detailed explanation of the following code snippet:\n```{code_snippet}```"})
27
-
28
- response = ""
29
-
30
- # Generate the response using the model
31
- for msg in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = msg.choices[0].delta.content
39
- response += token
40
- yield response
41
-
42
- # Example inputs as nested lists
43
- examples = [
44
- ["x = 5\nprint(x)"], # Simple Variable Assignment
45
- ["def add(a, b):\n return a + b\nresult = add(2, 3)\nprint(result)"], # Function Definition
46
- ["for i in range(5):\n print(i)"], # Looping Through a Range
47
- ["if x > 0:\n print('Positive')\nelse:\n print('Non-positive')"], # Conditional Statement
48
- ["squares = [i**2 for i in range(10)]\nprint(squares)"], # List Comprehension
49
- ]
50
-
51
- # Create the Gradio Chat Interface
52
- demo = gr.ChatInterface(
53
- explain_code,
54
- additional_inputs=[
55
- gr.Textbox(value="You are an expert assistant that explains Python code snippets clearly and concisely.", label="System message"),
56
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
57
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
58
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
59
- ],
60
- examples=examples,
61
  )
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
65
-
 
1
  import gradio as gr
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():
17
+ global x
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:
48
+ elapsed_time = time.time() - start_time
49
+ print(f"Processing time: {elapsed_time:.2f} seconds")
50
+
51
+ # Define the Gradio interface with multi-line input
52
+ iface = gr.Interface(
53
+ fn=get_completion,
54
+ inputs=[gr.Textbox(label="Insert Code Snippet", lines=10)], # Increased lines for multi-line input
55
+ outputs=[gr.Textbox(label="Explanation Here", lines=8)],
56
+ title="Code Explainer"
 
 
 
 
57
  )
58
 
59
+ iface.launch()