Chad commited on
Commit
9cf2f4c
·
1 Parent(s): c8103ec

fix example maybe

Browse files
Files changed (1) hide show
  1. app.py +31 -30
app.py CHANGED
@@ -2,19 +2,32 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
 
5
- # Set your API key using an environment variable
6
- api_key = os.getenv("GROQ_API_KEY")
 
 
 
 
 
 
 
 
7
 
 
 
8
  if not api_key:
9
  raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
10
-
11
- # Initialize Groq client
12
  client = Groq(api_key=api_key)
13
 
14
  def generate_reply(user_input, chat_history):
 
 
 
 
15
  if chat_history is None:
16
  chat_history = []
17
 
 
18
  conversation = [{"role": "system", "content": (
19
  "Determine the language of the user's question and respond in the same language. "
20
  "Users may switch between English and French, so maintain consistency. "
@@ -23,15 +36,15 @@ def generate_reply(user_input, chat_history):
23
  "(or the equivalent in French, depending on the input language). Then stop responding."
24
  )}]
25
 
 
26
  for entry in chat_history:
27
  if entry["role"] == "user":
28
  conversation.append({"role": "user", "content": entry["content"]})
29
  elif entry["role"] == "assistant":
30
  conversation.append({"role": "assistant", "content": entry["content"]})
31
-
32
-
33
  conversation.append({"role": "user", "content": user_input})
34
 
 
35
  try:
36
  response = client.chat.completions.create(
37
  model="llama3-70b-8192",
@@ -39,44 +52,29 @@ def generate_reply(user_input, chat_history):
39
  temperature=0.7,
40
  max_completion_tokens=512,
41
  top_p=0.95,
42
- stream=False,
43
  stop=None,
44
  )
45
  reply = response.choices[0].message.content or "..."
46
  except Exception as error:
47
  reply = f"Error: {error}"
48
 
 
49
  chat_history.append({"role": "user", "content": user_input})
50
  chat_history.append({"role": "assistant", "content": reply})
51
  return chat_history, chat_history, ""
52
 
 
 
 
 
53
  with gr.Blocks() as interface:
54
  gr.Markdown("## History Chatbot")
55
- chatbot = gr.Chatbot(type="messages")
56
- state = gr.State([])
57
  user_input = gr.Textbox(placeholder="Enter your question here...")
58
  send_button = gr.Button("Send")
59
 
60
- examples=[
61
- ["How did the Leaning Tower of Pisa start leaning?"],
62
- ["Comment la tour de Pise a-t-elle commencé à pencher ?"],
63
- ["Who built the Colosseum?"],
64
- ["Qui a construit le Colisée ?"],
65
- ["What caused the fall of the Roman Empire?"],
66
- ["Quelles sont les causes de la chute de l'Empire romain ?"],
67
- ["When did World War II begin and end?"],
68
- ["Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?"],
69
- ["What was the significance of the Magna Carta?"],
70
- ["Quelle est l'importance de la Magna Carta ?"],
71
- ["Who was the first person to step on the moon?"],
72
- ["Qui était la première personne à marcher sur la lune?"]
73
- ]
74
-
75
- gr.Examples(
76
- examples=examples,
77
- inputs=user_input
78
- )
79
-
80
  send_button.click(
81
  fn=generate_reply,
82
  inputs=[user_input, state],
@@ -84,7 +82,10 @@ with gr.Blocks() as interface:
84
  )
85
 
86
  reset_button = gr.Button("New Conversation")
87
- reset_button.click(lambda: [], outputs=[state])
 
 
 
88
 
89
  if __name__ == "__main__":
90
  interface.launch(share=True)
 
2
  import gradio as gr
3
  from groq import Groq
4
 
5
+ # Generic example prompts
6
+ EXAMPLES = [
7
+ {"role": "assistant", "content": "Here are some example questions you can ask:"},
8
+ {"role": "user", "content": "How did the Leaning Tower of Pisa start leaning?"},
9
+ {"role": "user", "content": "Who built the Colosseum?"},
10
+ {"role": "user", "content": "What caused the fall of the Roman Empire?"},
11
+ {"role": "user", "content": "When did World War II begin and end?"},
12
+ {"role": "user", "content": "What was the significance of the Magna Carta?"},
13
+ {"role": "user", "content": "Who was the first person to step on the moon?"},
14
+ ]
15
 
16
+ # Using Groq AI API
17
+ api_key = os.getenv("GROQ_API_KEY")
18
  if not api_key:
19
  raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
 
 
20
  client = Groq(api_key=api_key)
21
 
22
  def generate_reply(user_input, chat_history):
23
+ '''
24
+ Generates a reply to the user's input prompt
25
+ '''
26
+ # Keep a history of the chat
27
  if chat_history is None:
28
  chat_history = []
29
 
30
+ # Basic ruleset for Groq AI to follow
31
  conversation = [{"role": "system", "content": (
32
  "Determine the language of the user's question and respond in the same language. "
33
  "Users may switch between English and French, so maintain consistency. "
 
36
  "(or the equivalent in French, depending on the input language). Then stop responding."
37
  )}]
38
 
39
+ # Distinguishing between user and AI messages in conversation history
40
  for entry in chat_history:
41
  if entry["role"] == "user":
42
  conversation.append({"role": "user", "content": entry["content"]})
43
  elif entry["role"] == "assistant":
44
  conversation.append({"role": "assistant", "content": entry["content"]})
 
 
45
  conversation.append({"role": "user", "content": user_input})
46
 
47
+ # Generating latest response
48
  try:
49
  response = client.chat.completions.create(
50
  model="llama3-70b-8192",
 
52
  temperature=0.7,
53
  max_completion_tokens=512,
54
  top_p=0.95,
55
+ stream=True,
56
  stop=None,
57
  )
58
  reply = response.choices[0].message.content or "..."
59
  except Exception as error:
60
  reply = f"Error: {error}"
61
 
62
+ # Appending latest prompt and response to conversation history
63
  chat_history.append({"role": "user", "content": user_input})
64
  chat_history.append({"role": "assistant", "content": reply})
65
  return chat_history, chat_history, ""
66
 
67
+ def reset_conversation():
68
+ return EXAMPLES
69
+
70
+ # UI
71
  with gr.Blocks() as interface:
72
  gr.Markdown("## History Chatbot")
73
+ chatbot = gr.Chatbot(value=EXAMPLES, type="messages")
74
+ state = gr.State(EXAMPLES)
75
  user_input = gr.Textbox(placeholder="Enter your question here...")
76
  send_button = gr.Button("Send")
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  send_button.click(
79
  fn=generate_reply,
80
  inputs=[user_input, state],
 
82
  )
83
 
84
  reset_button = gr.Button("New Conversation")
85
+ reset_button.click(
86
+ fn=reset_conversation,
87
+ outputs=[chatbot, state]
88
+ )
89
 
90
  if __name__ == "__main__":
91
  interface.launch(share=True)