Alejadro Sanchez-Giraldo commited on
Commit
192cb9d
·
1 Parent(s): 30bd2eb
Files changed (3) hide show
  1. .gitignore +7 -0
  2. README.md +25 -1
  3. app.py +46 -50
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ dschatbot/
2
+ .DS_Store
3
+ .env
4
+
5
+ __pycache__/
6
+
7
+ flagged
README.md CHANGED
@@ -11,4 +11,28 @@ license: mit
11
  short_description: This is a simple chatbot to generte code using DeepSeeK
12
  ---
13
 
14
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  short_description: This is a simple chatbot to generte code using DeepSeeK
12
  ---
13
 
14
+ An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
15
+
16
+
17
+ This is a chatbot that interacts with the Fantasy Premier League (FPL) API to provide information about players, teams, and stats.
18
+
19
+ ```bash
20
+ python3 -m venv dschatbot
21
+ source dschatbot/bin/activate
22
+ ```
23
+
24
+ ## Installation
25
+
26
+ 1. Install the dependencies:
27
+
28
+ ```bash
29
+ pip install -r requirements.txt
30
+ ```
31
+
32
+ 2. Run the chatbot:
33
+
34
+ ```bash
35
+ python app.py
36
+ ```
37
+
38
+ ## Usage
app.py CHANGED
@@ -1,64 +1,60 @@
 
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"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import torch
5
 
6
+ tokenizer = AutoTokenizer.from_pretrained(
7
+ "deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ "deepseek-ai/deepseek-coder-1.3b-instruct", trust_remote_code=True, torch_dtype=torch.bfloat16)
10
 
11
+ # Disable tokenizers parallelism warning
12
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
13
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # Use CPU if CUDA is not available
16
+ device = torch.device("cpu" if not torch.cuda.is_available() else "cuda")
17
+ model = model.to(device)
 
 
18
 
19
+ # Theme builder
20
+ # gr.themes.builder()
21
 
22
+ theme = gr.themes.Soft(
23
+ primary_hue="sky",
24
+ neutral_hue="slate",
25
+ )
26
 
27
+ # Function to handle user input and generate a response
 
 
 
 
 
 
 
28
 
 
 
29
 
30
+ def chatbot_response(query):
31
+ response = "Lets see what I can do for you!"
32
+ # if response if a JSON boject iterate over the elements and conver is a list like "a": "b" "/n" "c": "d"
33
+ if isinstance(response, dict):
34
+ response = "\n".join(
35
+ [f"{key}: {value}" for key, value in response.items()])
36
 
37
+ # Generate response using the model
38
+ messages = [{'role': 'user', 'content': query}]
39
+ inputs = tokenizer.apply_chat_template(
40
+ messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
41
+
42
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=True, top_k=50,
43
+ top_p=0.95, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
44
+ model_response = tokenizer.decode(
45
+ outputs[0][len(inputs[0]):], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
46
 
47
+ return response + "\n\n" + model_response
48
+
49
+
50
+ # Set up the Gradio interface
51
+ iface = gr.Interface(
52
+ fn=chatbot_response,
53
+ inputs=gr.Textbox(label="Ask our DSChatbot Expert"),
54
+ outputs=gr.Textbox(label="Hope it helps!"),
55
+ theme=theme,
56
+ title="DSChatbot"
57
+ )
58
 
59
  if __name__ == "__main__":
60
+ iface.launch()