Chad commited on
Commit
806374e
·
1 Parent(s): 9919787
Files changed (3) hide show
  1. app.py +33 -17
  2. appConfig.py +28 -0
  3. requirements.txt +3 -1
app.py CHANGED
@@ -1,14 +1,26 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
5
 
6
  def respond(message, history):
7
  if history is None:
8
  history = []
9
 
10
- messages = [{"role": "system", "content": "The first thing to do is see what language the question was asked in and then answer in the same language, a user can alternate between english and french, make sure to answer in the language the question was just sent in. Only answer questions strictly related to history. If asked about any other topic, respond with: 'I am a history chatbot and can only answer history-related questions.' (or the french version if the user promt was in french) and then stop typing, do not continue to answer after you've sent that sentence. "}]
11
-
 
 
 
 
 
 
12
  for val in history:
13
  if val[0]:
14
  messages.append({"role": "user", "content": val[0]})
@@ -17,17 +29,22 @@ def respond(message, history):
17
 
18
  messages.append({"role": "user", "content": message})
19
 
20
- response = ""
21
- for message in client.chat_completion(
22
- messages,
23
- max_tokens=512,
24
- stream=True,
25
- temperature=0.7,
26
- top_p=0.95,
27
- ):
28
- token = message.choices[0].delta.content
29
- response += token
30
- yield response
 
 
 
 
 
31
 
32
  with gr.Blocks() as demo:
33
  with gr.Row():
@@ -49,7 +66,7 @@ with gr.Blocks() as demo:
49
  ["What was the significance of the Magna Carta?"],
50
  ["Quelle est l'importance de la Magna Carta ?"],
51
  ["Who was the first person to step on the moon?"],
52
- ["Qui était la première personne a marcher sur la lune?"]
53
  ],
54
  cache_examples=False
55
  )
@@ -59,4 +76,3 @@ with gr.Blocks() as demo:
59
 
60
  if __name__ == "__main__":
61
  demo.launch(share=True)
62
-
 
1
  import gradio as gr
2
+ import os
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(message, history):
13
  if history is None:
14
  history = []
15
 
16
+ messages = [{"role": "system", "content": (
17
+ "Determine the language of the user's question and respond in the same language. "
18
+ "Users may switch between English and French, so maintain consistency. "
19
+ "Only answer history-related questions. If asked about another topic, reply with: "
20
+ "'I am a history chatbot and can only answer history-related questions.' "
21
+ "(or the equivalent in French, depending on the input language). Then stop responding."
22
+ )}]
23
+
24
  for val in history:
25
  if val[0]:
26
  messages.append({"role": "user", "content": val[0]})
 
29
 
30
  messages.append({"role": "user", "content": message})
31
 
32
+ try:
33
+ response = client.chat.completions.create(
34
+ model="llama3-70b-8192",
35
+ messages=messages,
36
+ temperature=0.7,
37
+ max_completion_tokens=512,
38
+ top_p=0.95,
39
+ stream=False,
40
+ stop=None,
41
+ )
42
+ reply = response.choices[0].message.content or "..."
43
+ except Exception as error:
44
+ reply = f"Error: {error}"
45
+
46
+ history.append((message, reply))
47
+ return history, ""
48
 
49
  with gr.Blocks() as demo:
50
  with gr.Row():
 
66
  ["What was the significance of the Magna Carta?"],
67
  ["Quelle est l'importance de la Magna Carta ?"],
68
  ["Who was the first person to step on the moon?"],
69
+ ["Qui était la première personne à marcher sur la lune?"]
70
  ],
71
  cache_examples=False
72
  )
 
76
 
77
  if __name__ == "__main__":
78
  demo.launch(share=True)
 
appConfig.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+
4
+ api_key = os.getenv("GROQ_API_KEY")
5
+
6
+ if not api_key:
7
+ raise ValueError("GROQ_API_KEY is not set. Please set it as an environment variable.")
8
+
9
+ client = Groq(api_key=api_key)
10
+
11
+ def liveChat(messages):
12
+ try:
13
+ completion = client.chat.completions.create(
14
+ model="llama3-70b-8192",
15
+ messages=messages,
16
+ temperature=0.7,
17
+ max_completion_tokens=512,
18
+ top_p=0.95,
19
+ stream=True,
20
+ stop=None,
21
+ )
22
+ except Exception as e:
23
+ yield f"Error: {e}"
24
+ return
25
+
26
+ for chunk in completion:
27
+ token = chunk.choices[0].delta.content or ""
28
+ yield token
requirements.txt CHANGED
@@ -1 +1,3 @@
1
- huggingface_hub==0.25.2
 
 
 
1
+ huggingface_hub==0.25.2
2
+ gradio
3
+ groq