Chad commited on
Commit
e53eee7
·
1 Parent(s): 1965b20
Files changed (2) hide show
  1. app.py +43 -31
  2. deprecated.py +80 -0
app.py CHANGED
@@ -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
 
@@ -36,7 +41,7 @@ def respond(user_input, chat_history):
36
  temperature=0.7,
37
  max_completion_tokens=512,
38
  top_p=0.95,
39
- stream=True,
40
  stop=None,
41
  )
42
  reply = response.choices[0].message.content or "..."
@@ -45,36 +50,43 @@ def respond(user_input, chat_history):
45
 
46
  chat_history.append({"role": "user", "content": user_input})
47
  chat_history.append({"role": "assistant", "content": reply})
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
 
 
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
 
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="tuple")
58
+ state = gr.State([])
59
+ user_input = gr.Textbox(placeholder="Enter your question here...")
60
+ send_button = gr.Button("Send")
61
+
62
+ examples = [
63
+ "How did the Leaning Tower of Pisa start leaning?",
64
+ "Comment la tour de Pise a-t-elle commencé à pencher ?",
65
+ "Who built the Colosseum?",
66
+ "Qui a construit le Colisée ?",
67
+ "What caused the fall of the Roman Empire?",
68
+ "Quelles sont les causes de la chute de l'Empire romain ?",
69
+ "When did World War II begin and end?",
70
+ "Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?",
71
+ "What was the significance of the Magna Carta?",
72
+ "Quelle est l'importance de la Magna Carta ?",
73
+ "Who was the first person to step on the moon?",
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
  )
87
 
88
+ reset_button = gr.Button("New Conversation")
89
+ reset_button.click(lambda: [], outputs=[state])
90
 
91
  if __name__ == "__main__":
92
+ interface.launch(share=True)
deprecated.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+
5
+ api_key = os.getenv("GROQ_API_KEY")
6
+
7
+ if not api_key:
8
+ raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
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. "
21
+ "Users may switch between English and French, so maintain consistency. "
22
+ "Only answer history-related questions. If asked about another topic, reply with: "
23
+ "'I am a history chatbot and can only answer history-related questions.' "
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
+
32
+ try:
33
+ response = client.chat.completions.create(
34
+ model="llama3-70b-8192",
35
+ messages=conversation,
36
+ temperature=0.7,
37
+ max_completion_tokens=512,
38
+ top_p=0.95,
39
+ stream=True,
40
+ stop=None,
41
+ )
42
+ reply = response.choices[0].message.content or "..."
43
+ except Exception as error:
44
+ reply = f"Error: {error}"
45
+
46
+ chat_history.append({"role": "user", "content": user_input})
47
+ chat_history.append({"role": "assistant", "content": reply})
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)