Wosqa commited on
Commit
6b87b35
·
verified ·
1 Parent(s): 48f2381

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -13,25 +13,24 @@ Explain programming concepts in a simple, beginner-friendly way.
13
  Provide examples when helpful and keep answers concise.
14
  """
15
 
16
- # Build messages in proper format
17
- def build_messages(user_input, chat_history):
18
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
19
 
20
- # Add previous chat history safely
21
  for item in chat_history or []:
22
  if isinstance(item, tuple) and len(item) == 2:
23
- user_msg = str(item[0])
24
- bot_msg = str(item[1])
25
- messages.append({"role": "user", "content": user_msg})
26
- messages.append({"role": "assistant", "content": bot_msg})
27
 
28
- # Add latest user input
29
  messages.append({"role": "user", "content": str(user_input)})
30
  return messages
31
 
32
  # Call GROQ API
33
  def query_groq(user_input, chat_history, temperature):
34
- messages = build_messages(user_input, chat_history)
35
 
36
  headers = {
37
  "Authorization": f"Bearer {GROQ_API_KEY}",
@@ -53,14 +52,9 @@ def query_groq(user_input, chat_history, temperature):
53
 
54
  # Gradio respond function
55
  def respond(user_input, chat_history, temperature):
56
- safe_history = []
57
- for item in chat_history or []:
58
- if isinstance(item, tuple) and len(item) == 2:
59
- safe_history.append((str(item[0]), str(item[1])))
60
-
61
- bot_reply = query_groq(user_input, safe_history, temperature)
62
- safe_history.append((str(user_input), str(bot_reply)))
63
- return "", safe_history
64
 
65
  # Gradio UI
66
  with gr.Blocks() as demo:
@@ -70,7 +64,13 @@ with gr.Blocks() as demo:
70
  state = gr.State([])
71
 
72
  msg = gr.Textbox(label="Ask a programming question")
73
- temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Response Creativity")
 
 
 
 
 
 
74
  clear = gr.Button("Clear Chat")
75
 
76
  msg.submit(respond, [msg, state, temperature], [msg, chatbot])
 
13
  Provide examples when helpful and keep answers concise.
14
  """
15
 
16
+ # Build messages for GROQ API
17
+ def build_groq_messages(user_input, chat_history):
18
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
19
 
20
+ # Convert Gradio history tuples into dicts for API
21
  for item in chat_history or []:
22
  if isinstance(item, tuple) and len(item) == 2:
23
+ user_msg, bot_msg = item
24
+ messages.append({"role": "user", "content": str(user_msg)})
25
+ messages.append({"role": "assistant", "content": str(bot_msg)})
 
26
 
27
+ # Latest user input
28
  messages.append({"role": "user", "content": str(user_input)})
29
  return messages
30
 
31
  # Call GROQ API
32
  def query_groq(user_input, chat_history, temperature):
33
+ messages = build_groq_messages(user_input, chat_history)
34
 
35
  headers = {
36
  "Authorization": f"Bearer {GROQ_API_KEY}",
 
52
 
53
  # Gradio respond function
54
  def respond(user_input, chat_history, temperature):
55
+ bot_reply = query_groq(user_input, chat_history, temperature)
56
+ new_history = (chat_history or []) + [(user_input, bot_reply)]
57
+ return "", new_history
 
 
 
 
 
58
 
59
  # Gradio UI
60
  with gr.Blocks() as demo:
 
64
  state = gr.State([])
65
 
66
  msg = gr.Textbox(label="Ask a programming question")
67
+ temperature = gr.Slider(
68
+ minimum=0.1,
69
+ maximum=1.0,
70
+ value=0.7,
71
+ step=0.1,
72
+ label="Response Creativity"
73
+ )
74
  clear = gr.Button("Clear Chat")
75
 
76
  msg.submit(respond, [msg, state, temperature], [msg, chatbot])