drewvid commited on
Commit
ad4bede
·
1 Parent(s): 5a8e12c

modernised code

Browse files
Files changed (2) hide show
  1. app.py +17 -13
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
@@ -5,7 +6,10 @@ from huggingface_hub import InferenceClient
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
  # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
- client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
 
 
 
9
 
10
 
11
  def respond(
@@ -26,9 +30,9 @@ def respond(
26
  4) Speech Acts.
27
 
28
  Provide detailed, patient explanations and guidance on language usage, grammar, vocabulary, and communication skills within the context of the above theories. Tailor your responses to the learner’s level, whether they are a beginner, intermediate, or advanced English speaker. Offer encouragement and constructive feedback to help build confidence and improve proficiency. Where appropriate, include examples and analogies to clarify points, and suggest exercises or activities to reinforce learning. Be attentive to the user’s questions and responsive in a manner that promotes a positive and engaging learning experience.
29
-
30
- Example user prompts are:
31
-
32
  1) "Please create a Language Frame for a visit to the dentist with useful phrases and vocabulary grouped according to roles, speech acts, and functional grammar.";
33
  2) "Please create a Language Frame for going skiing with useful phrases and vocabulary grouped according to speech acts and roles.";
34
  3) "I am planning a trip to a foreign country. Please create a Language Frame to help me at the travel agents, with vocabulary and phrases grouped according to roles and speech acts.";
@@ -36,7 +40,7 @@ def respond(
36
  5) "Who are you?".
37
 
38
  """
39
-
40
  messages = [{"role": "system", "content": system_message}]
41
 
42
  for val in history:
@@ -49,20 +53,20 @@ def respond(
49
 
50
  response = ""
51
 
52
- for message in client.chat_completion(
 
53
  messages,
54
  max_tokens=max_tokens,
55
- stream=True,
56
  temperature=temperature,
57
  top_p=top_p,
 
58
  ):
59
- if message.choices:
60
- token = message.choices[0].delta.content
61
- if token:
62
- response += token
63
  yield response
64
- else:
65
- yield "Please clear the history and try again."
66
 
67
  """
68
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
1
+ import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
 
6
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
  """
8
  # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
+ client = InferenceClient(
10
+ "meta-llama/Meta-Llama-3-8B-Instruct",
11
+ token=os.environ.get("HF_TOKEN")
12
+ )
13
 
14
 
15
  def respond(
 
30
  4) Speech Acts.
31
 
32
  Provide detailed, patient explanations and guidance on language usage, grammar, vocabulary, and communication skills within the context of the above theories. Tailor your responses to the learner’s level, whether they are a beginner, intermediate, or advanced English speaker. Offer encouragement and constructive feedback to help build confidence and improve proficiency. Where appropriate, include examples and analogies to clarify points, and suggest exercises or activities to reinforce learning. Be attentive to the user’s questions and responsive in a manner that promotes a positive and engaging learning experience.
33
+
34
+ Example user prompts are:
35
+
36
  1) "Please create a Language Frame for a visit to the dentist with useful phrases and vocabulary grouped according to roles, speech acts, and functional grammar.";
37
  2) "Please create a Language Frame for going skiing with useful phrases and vocabulary grouped according to speech acts and roles.";
38
  3) "I am planning a trip to a foreign country. Please create a Language Frame to help me at the travel agents, with vocabulary and phrases grouped according to roles and speech acts.";
 
40
  5) "Who are you?".
41
 
42
  """
43
+
44
  messages = [{"role": "system", "content": system_message}]
45
 
46
  for val in history:
 
53
 
54
  response = ""
55
 
56
+ # Stream the model output safely
57
+ for msg in client.chat_completion(
58
  messages,
59
  max_tokens=max_tokens,
 
60
  temperature=temperature,
61
  top_p=top_p,
62
+ stream=True,
63
  ):
64
+ if hasattr(msg, "choices") and msg.choices:
65
+ delta = msg.choices[0].delta
66
+ if hasattr(delta, "content") and delta.content:
67
+ response += delta.content
68
  yield response
69
+ # Ignore any events that do not contain content
 
70
 
71
  """
72
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- huggingface_hub==0.22.2
 
 
1
+ gradio>=4.44.0
2
+ huggingface_hub>=0.23.0