tharun1507 commited on
Commit
ee08060
·
verified ·
1 Parent(s): f5a4da5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -9
app.py CHANGED
@@ -1,16 +1,14 @@
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_and_run_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
  explanation = ""
11
  output = ""
12
 
13
- # Generate the explanation using the model
14
  try:
15
  messages = [{"role": "system", "content": system_message}]
16
  messages.append({
@@ -25,16 +23,14 @@ def explain_and_run_code(code_snippet):
25
  temperature=0.7,
26
  top_p=0.95
27
  ):
28
- token = msg["choices"][0]["delta"]["content"]
29
  explanation += token
30
  except Exception as e:
31
  explanation = f"An error occurred during explanation: {str(e)}"
32
 
33
- # Execute the code snippet and capture the output
34
  try:
35
  import io
36
  import contextlib
37
-
38
  output_buffer = io.StringIO()
39
  with contextlib.redirect_stdout(output_buffer):
40
  exec(code_snippet)
@@ -44,12 +40,13 @@ def explain_and_run_code(code_snippet):
44
 
45
  return f"**Explanation:**\n{explanation}\n\n**Output:**\n{output}"
46
 
47
- # Create the Gradio Interface with centered title
 
48
  demo = gr.Interface(
49
  fn=explain_and_run_code,
50
  inputs=gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
51
  outputs=gr.Textbox(label="Explanation and Output", lines=15),
52
- title="DECIPHER The Python Code Explainer\n\n Al Capstone Project\n (XII-C)",
53
  theme="default"
54
  )
55
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_api_token)
5
+
6
 
7
  def explain_and_run_code(code_snippet):
 
8
  system_message = "You are an expert assistant that explains Python code snippets clearly and concisely."
9
  explanation = ""
10
  output = ""
11
 
 
12
  try:
13
  messages = [{"role": "system", "content": system_message}]
14
  messages.append({
 
23
  temperature=0.7,
24
  top_p=0.95
25
  ):
26
+ token = msg["choices"][0]["delta"].get("content", "")
27
  explanation += token
28
  except Exception as e:
29
  explanation = f"An error occurred during explanation: {str(e)}"
30
 
 
31
  try:
32
  import io
33
  import contextlib
 
34
  output_buffer = io.StringIO()
35
  with contextlib.redirect_stdout(output_buffer):
36
  exec(code_snippet)
 
40
 
41
  return f"**Explanation:**\n{explanation}\n\n**Output:**\n{output}"
42
 
43
+
44
+ # Gradio Interface
45
  demo = gr.Interface(
46
  fn=explain_and_run_code,
47
  inputs=gr.Textbox(placeholder="Enter your code snippet here...", label="Code Snippet", lines=10),
48
  outputs=gr.Textbox(label="Explanation and Output", lines=15),
49
+ title="DECIPHER The Python Code Explainer\n\n AI Capstone Project\n (XII-C)",
50
  theme="default"
51
  )
52