tharun1507 commited on
Commit
0fe7900
·
verified ·
1 Parent(s): 5cb0f71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -36
app.py CHANGED
@@ -4,29 +4,26 @@ from huggingface_hub import InferenceClient
4
  # Initialize the Hugging Face Inference Client
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
- def explain_code(
8
- code_snippet,
9
- system_message,
10
- max_tokens,
11
- temperature,
12
- top_p
13
- ):
14
- # Prepare messages for the model
15
- messages = [{"role": "system", "content": system_message}]
16
- messages.append({
17
- "role": "user",
18
- "content": f"Please provide a detailed explanation of the following code snippet:\n```{code_snippet}```"
19
- })
20
-
21
  response = ""
 
22
  try:
 
 
 
 
 
 
 
23
  # Generate the response using the model
24
  for msg in client.chat_completion(
25
  messages,
26
- max_tokens=max_tokens,
27
  stream=True,
28
- temperature=temperature,
29
- top_p=top_p
30
  ):
31
  token = msg["choices"][0]["delta"]["content"]
32
  response += token
@@ -35,28 +32,13 @@ def explain_code(
35
 
36
  return response
37
 
38
- # Example inputs
39
- examples = [
40
- ["x = 5\nprint(x)"],
41
- ["def add(a, b):\n return a + b\nresult = add(2, 3)\nprint(result)"],
42
- ["for i in range(5):\n print(i)"],
43
- ["if x > 0:\n print('Positive')\nelse:\n print('Non-positive')"],
44
- ["squares = [i**2 for i in range(10)]\nprint(squares)"]
45
- ]
46
-
47
- # Create the Gradio Interface
48
  demo = gr.Interface(
49
  fn=explain_code,
50
- inputs=[
51
- gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
52
- gr.Textbox(value="You are an expert assistant that explains Python code snippets clearly and concisely.", label="System message"),
53
- gr.Slider(1, 2048, 512, step=1, label="Max new tokens"),
54
- gr.Slider(0.1, 4.0, 0.7, step=0.1, label="Temperature"),
55
- gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p (nucleus sampling)")
56
- ],
57
  outputs=gr.Textbox(label="Explanation Here", lines=10),
58
- examples=examples,
59
- title="Python Code Explainer"
60
  )
61
 
62
  if __name__ == "__main__":
 
4
  # Initialize the Hugging Face Inference Client
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
+ def explain_code(code_snippet):
8
+ # Prepare a basic system message
9
+ system_message = "You are an expert assistant that explains Python code snippets clearly and concisely."
 
 
 
 
 
 
 
 
 
 
 
10
  response = ""
11
+
12
  try:
13
+ # Prepare the messages for the model
14
+ messages = [{"role": "system", "content": system_message}]
15
+ messages.append({
16
+ "role": "user",
17
+ "content": f"Please provide a detailed explanation of the following code snippet:\n```{code_snippet}```"
18
+ })
19
+
20
  # Generate the response using the model
21
  for msg in client.chat_completion(
22
  messages,
23
+ max_tokens=2047,
24
  stream=True,
25
+ temperature=0.7,
26
+ top_p=0.95
27
  ):
28
  token = msg["choices"][0]["delta"]["content"]
29
  response += token
 
32
 
33
  return response
34
 
35
+ # Create the Gradio Interface with only the necessary components
 
 
 
 
 
 
 
 
 
36
  demo = gr.Interface(
37
  fn=explain_code,
38
+ inputs=gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
 
 
 
 
 
 
39
  outputs=gr.Textbox(label="Explanation Here", lines=10),
40
+ title="Python Code Explainer",
41
+ theme="default"
42
  )
43
 
44
  if __name__ == "__main__":