nikravan commited on
Commit
eb06650
Β·
verified Β·
1 Parent(s): 64d8d14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -64
app.py CHANGED
@@ -1,70 +1,35 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- @spaces.GPU
6
- def respond(
7
- message,
8
- history: list[dict[str, str]],
9
- system_message,
10
- max_tokens,
11
- temperature,
12
- top_p,
13
- hf_token: gr.OAuthToken,
14
- ):
15
- """
16
- 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
17
- """
18
- client = InferenceClient(token=hf_token.token, model="sapientinc/HRM-Text-1B")
19
-
20
- messages = [{"role": "system", "content": system_message}]
21
-
22
- messages.extend(history)
23
-
24
- messages.append({"role": "user", "content": message})
25
-
26
- response = ""
27
-
28
- for message in client.chat_completion(
29
- messages,
30
- max_tokens=max_tokens,
31
- stream=True,
32
- temperature=temperature,
33
- top_p=top_p,
34
- ):
35
- choices = message.choices
36
- token = ""
37
- if len(choices) and choices[0].delta.content:
38
- token = choices[0].delta.content
39
-
40
- response += token
41
- yield response
42
-
43
-
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
47
  chatbot = gr.ChatInterface(
48
- respond,
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
  )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
68
-
69
  if __name__ == "__main__":
70
- demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from spaces import GPU
5
+
6
+ model_name = "microsoft/DialoGPT-small"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ model.to(device)
12
+
13
+ @GPU
14
+ def generate_response(message, history):
15
+ input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors="pt").to(device)
16
+ chat_history_ids = input_ids
17
+ response_ids = model.generate(
18
+ chat_history_ids,
19
+ max_length=200,
20
+ pad_token_id=tokenizer.eos_token_id,
21
+ do_sample=True,
22
+ temperature=0.7
23
+ )
24
+ response = tokenizer.decode(response_ids[:, chat_history_ids.shape[-1]:][0], skip_special_tokens=True)
25
+ return response.strip()
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  chatbot = gr.ChatInterface(
28
+ fn=generate_response,
29
+ title="Simple ZeroGPU Chatbot",
30
+ description="A simple chatbot using DialoGPT, running on Hugging Face ZeroGPU.",
31
+ examples=["Hello!", "How are you today?", "What's the latest news?"]
 
 
 
 
 
 
 
 
 
32
  )
33
 
 
 
 
 
 
 
34
  if __name__ == "__main__":
35
+ chatbot.launch()