hannayukhymenko HF Staff commited on
Commit
e5bb798
·
verified ·
1 Parent(s): e3e3783

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -40
app.py CHANGED
@@ -1,49 +1,38 @@
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("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
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=[
49
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
@@ -57,8 +46,16 @@ demo = gr.ChatInterface(
57
  label="Top-p (nucleus sampling)",
58
  ),
59
  ],
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
 
 
 
 
5
 
6
+ model_name = "INSAIT-Institute/MamayLM-Gemma-2-9B-IT-v0.1"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
9
 
10
+ def respond(message, chat_history, system_message, max_new_tokens, temperature, top_p):
 
 
 
 
 
 
 
 
11
 
12
+ prompt = f"{system_message.strip()}\n"
13
+ for user, bot in chat_history:
14
+ prompt += f"User: {user}\nAssistant: {bot}\n"
15
+ prompt += f"User: {message}\nAssistant:"
 
16
 
17
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
18
+ output = model.generate(
19
+ **inputs,
20
+ max_new_tokens=int(max_new_tokens),
21
+ pad_token_id=tokenizer.eos_token_id,
22
+ do_sample=True,
23
+ temperature=float(temperature),
24
+ top_p=float(top_p),
25
+ eos_token_id=tokenizer.eos_token_id,
26
+ )
27
+ decoded = tokenizer.decode(output[0], skip_special_tokens=True)
28
 
29
+ response = decoded.split("Assistant:")[-1].strip().split("User:")[0].strip()
30
+ return response
31
 
32
+ def clear_fn():
33
+ return None
 
 
 
 
 
 
34
 
35
+ chat = gr.ChatInterface(
 
 
 
 
 
 
 
36
  respond,
37
  additional_inputs=[
38
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
 
46
  label="Top-p (nucleus sampling)",
47
  ),
48
  ],
49
+ examples=[
50
+ ["Привіт!"],
51
+ ["Хто такий Пес Патрон?"],
52
+ ],
53
+ title="💬 Chat with MamayLM",
54
+ description="A multi-turn chat interface for MamayLM-v0,1-9B with configurable parameters.",
55
+ theme="soft",
56
+ retry_btn="🔄 Retry",
57
+ undo_btn="↩️ Undo",
58
+ clear_btn="🗑️ Clear Chat",
59
  )
60
 
61
+ chat.launch()