xylin commited on
Commit
1df41f3
·
verified ·
1 Parent(s): 0bbe696

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -16,12 +16,12 @@ def format_steps(steps, total_time):
16
  md_content += f"{content}\n"
17
  md_content += f"_Thinking time for this step: {thinking_time:.2f} seconds_\n"
18
  md_content += "\n---\n"
19
- if total_time!=0:
20
  md_content += f"\n**Total thinking time: {total_time:.2f} seconds**"
21
  return md_content
22
 
23
- def main(api_key, user_query):
24
- if not api_key:
25
  yield "Please enter your Groq API key to proceed."
26
  return
27
 
@@ -30,8 +30,11 @@ def main(api_key, user_query):
30
  return
31
 
32
  try:
33
- # Initialize the Groq client with the provided API key
34
- client = groq.Groq(api_key=api_key)
 
 
 
35
  except Exception as e:
36
  yield f"Failed to initialize Groq client. Error: {str(e)}"
37
  return
@@ -56,10 +59,12 @@ with gr.Blocks() as demo:
56
 
57
  with gr.Row():
58
  with gr.Column():
 
59
  api_input = gr.Textbox(
60
  label="Enter your Groq API Key:",
61
  placeholder="Your Groq API Key",
62
- type="password"
 
63
  )
64
  user_input = gr.Textbox(
65
  label="Enter your query:",
@@ -73,8 +78,11 @@ with gr.Blocks() as demo:
73
  with gr.Column():
74
  output_md = gr.Markdown()
75
 
76
- submit_btn.click(fn=main, inputs=[api_input, user_input], outputs=output_md)
 
 
 
77
 
78
  # Launch the Gradio app
79
  if __name__ == "__main__":
80
- demo.launch()
 
16
  md_content += f"{content}\n"
17
  md_content += f"_Thinking time for this step: {thinking_time:.2f} seconds_\n"
18
  md_content += "\n---\n"
19
+ if total_time != 0:
20
  md_content += f"\n**Total thinking time: {total_time:.2f} seconds**"
21
  return md_content
22
 
23
+ def main(api_key, user_query, mode):
24
+ if mode == "private" and not api_key:
25
  yield "Please enter your Groq API key to proceed."
26
  return
27
 
 
30
  return
31
 
32
  try:
33
+ # Initialize the Groq client with the provided API key or the environment variable
34
+ if mode == "public":
35
+ client = groq.Groq(api_key=os.getenv("GROQ_API_KEY"))
36
+ else:
37
+ client = groq.Groq(api_key=api_key)
38
  except Exception as e:
39
  yield f"Failed to initialize Groq client. Error: {str(e)}"
40
  return
 
59
 
60
  with gr.Row():
61
  with gr.Column():
62
+ mode_toggle = gr.Radio(["public", "private"], label="API Key Mode", value="public")
63
  api_input = gr.Textbox(
64
  label="Enter your Groq API Key:",
65
  placeholder="Your Groq API Key",
66
+ type="password",
67
+ visible=False # Initially hidden
68
  )
69
  user_input = gr.Textbox(
70
  label="Enter your query:",
 
78
  with gr.Column():
79
  output_md = gr.Markdown()
80
 
81
+ # Show/hide the API key input based on the mode toggle
82
+ mode_toggle.change(lambda mode: gr.update(visible=mode == "private"), mode_toggle, api_input)
83
+
84
+ submit_btn.click(fn=main, inputs=[api_input, user_input, mode_toggle], outputs=output_md)
85
 
86
  # Launch the Gradio app
87
  if __name__ == "__main__":
88
+ demo.launch()