Chad commited on
Commit
2291d9c
·
1 Parent(s): f09bd6f

resetwdwdwdwdw

Browse files
Files changed (2) hide show
  1. app.py +18 -9
  2. deprecated.py → test.py +42 -29
app.py CHANGED
@@ -9,13 +9,12 @@ if not api_key:
9
 
10
  client = Groq(api_key=api_key)
11
 
12
- def generate_reply(user_input, chat_history):
13
  if chat_history is None:
14
  chat_history = []
15
 
16
  if isinstance(chat_history, list):
17
- chat_history = [(str(msg[0]), str(msg[1])) for msg in chat_history if isinstance(msg, (list, tuple)) and len(msg) == 2]
18
-
19
 
20
  conversation = [{"role": "system", "content": (
21
  "Determine the language of the user's question and respond in the same language. "
@@ -41,7 +40,7 @@ def generate_reply(user_input, chat_history):
41
  temperature=0.7,
42
  max_completion_tokens=512,
43
  top_p=0.95,
44
- stream=False,
45
  stop=None,
46
  )
47
  reply = response.choices[0].message.content or "..."
@@ -50,12 +49,16 @@ def generate_reply(user_input, chat_history):
50
 
51
  chat_history.append({"role": "user", "content": user_input})
52
  chat_history.append({"role": "assistant", "content": reply})
 
53
  return chat_history, chat_history, ""
54
 
55
  with gr.Blocks() as interface:
56
  gr.Markdown("## History Chatbot")
57
- chatbot = gr.Chatbot(type="messages")
58
- state = gr.State([])
 
 
 
59
  user_input = gr.Textbox(placeholder="Enter your question here...")
60
  send_button = gr.Button("Send")
61
 
@@ -74,13 +77,19 @@ with gr.Blocks() as interface:
74
  "Qui était la première personne à marcher sur la lune?"
75
  ]
76
 
 
 
 
 
77
  gr.Examples(
78
  examples=examples,
79
- inputs=user_input
 
 
80
  )
81
 
82
  send_button.click(
83
- fn=generate_reply,
84
  inputs=[user_input, state],
85
  outputs=[chatbot, state, user_input]
86
  )
@@ -89,4 +98,4 @@ with gr.Blocks() as interface:
89
  reset_button.click(lambda: [], outputs=[state])
90
 
91
  if __name__ == "__main__":
92
- interface.launch(share=True)
 
9
 
10
  client = Groq(api_key=api_key)
11
 
12
+ def respond(user_input, chat_history):
13
  if chat_history is None:
14
  chat_history = []
15
 
16
  if isinstance(chat_history, list):
17
+ chat_history = [{"role": msg["role"], "content": str(msg["content"])} for msg in chat_history if isinstance(msg, dict) and "role" in msg and "content" in msg]
 
18
 
19
  conversation = [{"role": "system", "content": (
20
  "Determine the language of the user's question and respond in the same language. "
 
40
  temperature=0.7,
41
  max_completion_tokens=512,
42
  top_p=0.95,
43
+ stream=True,
44
  stop=None,
45
  )
46
  reply = response.choices[0].message.content or "..."
 
49
 
50
  chat_history.append({"role": "user", "content": user_input})
51
  chat_history.append({"role": "assistant", "content": reply})
52
+
53
  return chat_history, chat_history, ""
54
 
55
  with gr.Blocks() as interface:
56
  gr.Markdown("## History Chatbot")
57
+
58
+ # Use 'messages' format for chatbot component
59
+ chatbot = gr.Chatbot(type="messages")
60
+ state = gr.State([]) # Store conversation history
61
+
62
  user_input = gr.Textbox(placeholder="Enter your question here...")
63
  send_button = gr.Button("Send")
64
 
 
77
  "Qui était la première personne à marcher sur la lune?"
78
  ]
79
 
80
+ # Ensure clicking an example sends the message
81
+ def process_example(example):
82
+ return example, state
83
+
84
  gr.Examples(
85
  examples=examples,
86
+ inputs=user_input,
87
+ outputs=[user_input, state], # Set input and preserve history
88
+ fn=process_example
89
  )
90
 
91
  send_button.click(
92
+ fn=respond,
93
  inputs=[user_input, state],
94
  outputs=[chatbot, state, user_input]
95
  )
 
98
  reset_button.click(lambda: [], outputs=[state])
99
 
100
  if __name__ == "__main__":
101
+ interface.launch()
deprecated.py → test.py RENAMED
@@ -9,12 +9,13 @@ if not api_key:
9
 
10
  client = Groq(api_key=api_key)
11
 
12
- def respond(user_input, chat_history):
13
  if chat_history is None:
14
  chat_history = []
15
 
16
  if isinstance(chat_history, list):
17
- chat_history = [{"role": msg["role"], "content": str(msg["content"])} for msg in chat_history if isinstance(msg, dict) and "role" in msg and "content" in msg]
 
18
 
19
  conversation = [{"role": "system", "content": (
20
  "Determine the language of the user's question and respond in the same language. "
@@ -24,8 +25,12 @@ def respond(user_input, chat_history):
24
  "(or the equivalent in French, depending on the input language). Then stop responding."
25
  )}]
26
 
27
- for msg in chat_history:
28
- conversation.append(msg)
 
 
 
 
29
 
30
  conversation.append({"role": "user", "content": user_input})
31
 
@@ -48,33 +53,41 @@ def respond(user_input, chat_history):
48
 
49
  return chat_history, chat_history, ""
50
 
51
- with gr.Blocks() as demo:
52
- with gr.Row():
53
- chatbot = gr.Chatbot(height=450, type="messages")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- chat = gr.ChatInterface(
56
- respond,
57
- chatbot=chatbot,
58
- additional_inputs=[],
59
- examples=[
60
- ["How did the Leaning Tower of Pisa start leaning?"],
61
- ["Comment la tour de Pise a-t-elle commencé à pencher ?"],
62
- ["Who built the Colosseum?"],
63
- ["Qui a construit le Colisée ?"],
64
- ["What caused the fall of the Roman Empire?"],
65
- ["Quelles sont les causes de la chute de l'Empire romain ?"],
66
- ["When did World War II begin and end?"],
67
- ["Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?"],
68
- ["What was the significance of the Magna Carta?"],
69
- ["Quelle est l'importance de la Magna Carta ?"],
70
- ["Who was the first person to step on the moon?"],
71
- ["Qui était la première personne à marcher sur la lune?"]
72
- ],
73
- cache_examples=False
74
  )
75
 
76
- clear_button = gr.Button("New/Nouvelle Conversation")
77
- clear_button.click(lambda: None, outputs=[chatbot], queue=False)
78
 
79
  if __name__ == "__main__":
80
- demo.launch(share=True)
 
9
 
10
  client = Groq(api_key=api_key)
11
 
12
+ def generate_reply(user_input, chat_history):
13
  if chat_history is None:
14
  chat_history = []
15
 
16
  if isinstance(chat_history, list):
17
+ chat_history = [(str(msg[0]), str(msg[1])) for msg in chat_history if isinstance(msg, (list, tuple)) and len(msg) == 2]
18
+
19
 
20
  conversation = [{"role": "system", "content": (
21
  "Determine the language of the user's question and respond in the same language. "
 
25
  "(or the equivalent in French, depending on the input language). Then stop responding."
26
  )}]
27
 
28
+ for entry in chat_history:
29
+ if entry["role"] == "user":
30
+ conversation.append({"role": "user", "content": entry["content"]})
31
+ elif entry["role"] == "assistant":
32
+ conversation.append({"role": "assistant", "content": entry["content"]})
33
+
34
 
35
  conversation.append({"role": "user", "content": user_input})
36
 
 
53
 
54
  return chat_history, chat_history, ""
55
 
56
+ with gr.Blocks() as interface:
57
+ gr.Markdown("## History Chatbot")
58
+ chatbot = gr.Chatbot(type="messages")
59
+ state = gr.State([])
60
+ user_input = gr.Textbox(placeholder="Enter your question here...")
61
+ send_button = gr.Button("Send")
62
+
63
+ examples = [
64
+ "How did the Leaning Tower of Pisa start leaning?",
65
+ "Comment la tour de Pise a-t-elle commencé à pencher ?",
66
+ "Who built the Colosseum?",
67
+ "Qui a construit le Colisée ?",
68
+ "What caused the fall of the Roman Empire?",
69
+ "Quelles sont les causes de la chute de l'Empire romain ?",
70
+ "When did World War II begin and end?",
71
+ "Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?",
72
+ "What was the significance of the Magna Carta?",
73
+ "Quelle est l'importance de la Magna Carta ?",
74
+ "Who was the first person to step on the moon?",
75
+ "Qui était la première personne à marcher sur la lune?"
76
+ ]
77
+
78
+ gr.Examples(
79
+ examples=examples,
80
+ inputs=user_input
81
+ )
82
 
83
+ send_button.click(
84
+ fn=generate_reply,
85
+ inputs=[user_input, state],
86
+ outputs=[chatbot, state, user_input]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  )
88
 
89
+ reset_button = gr.Button("New Conversation")
90
+ reset_button.click(lambda: [], outputs=[state])
91
 
92
  if __name__ == "__main__":
93
+ interface.launch(share=True)