TK17250 commited on
Commit
69cbbe9
·
verified ·
1 Parent(s): bca40c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -32
app.py CHANGED
@@ -1,11 +1,7 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
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("UpMath/Thai-HomeworkGen-v3")
8
 
 
9
 
10
  def respond(
11
  message,
@@ -15,34 +11,27 @@ def respond(
15
  temperature,
16
  top_p,
17
  ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
  temperature=temperature,
35
  top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
@@ -59,6 +48,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
 
 
 
 
3
 
4
+ pipe = pipeline(task="text-generation", model="UpMath/Thai-HomeworkGen-v3")
5
 
6
  def respond(
7
  message,
 
11
  temperature,
12
  top_p,
13
  ):
14
+ context = system_message + "\n"
15
+ for user, assistant in history:
16
+ if user:
17
+ context += f"User: {user}\n"
18
+ if assistant:
19
+ context += f"Assistant: {assistant}\n"
20
+ context += f"User: {message}\nAssistant:"
21
+
22
+ outputs = pipe(
23
+ context,
24
+ max_new_tokens=max_tokens,
 
 
 
 
 
25
  temperature=temperature,
26
  top_p=top_p,
27
+ do_sample=True,
28
+ )
 
 
 
29
 
30
+ response = outputs[0]["generated_text"]
31
+
32
+ response_only = response[len(context):].strip()
33
+ yield response_only
34
 
 
 
 
35
  demo = gr.ChatInterface(
36
  respond,
37
  additional_inputs=[
 
48
  ],
49
  )
50
 
 
51
  if __name__ == "__main__":
52
+ demo.launch()