Austin Stockbridge commited on
Commit
122221c
·
verified ·
1 Parent(s): bf9137e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -11
app.py CHANGED
@@ -1,10 +1,11 @@
1
  from huggingface_hub import hf_hub_list, hf_hub_download
2
  from transformers import AutoModel, AutoTokenizer
 
3
 
4
- # Hugging Face model repository
5
  model_repo = "mradermacher/Qwen2-2B-RepleteCoder-DHM-GGUF"
6
 
7
- # Find files containing 'Q2_K'
8
  def find_q2_k_files(repo_id):
9
  files = hf_hub_list(repo_id)
10
  q2_k_files = [file for file in files if "Q2_K" in file.filename and file.filename.endswith(".gguf")]
@@ -12,16 +13,57 @@ def find_q2_k_files(repo_id):
12
  raise ValueError(f"No files containing 'Q2_K' found in the repository {repo_id}.")
13
  return q2_k_files
14
 
15
- # Download and load the desired model
16
- def load_q2_k_model(repo_id):
17
- q2_k_files = find_q2_k_files(repo_id)
18
- # For simplicity, load the first matching file
19
- chosen_file = q2_k_files[0]
20
- print(f"Loading model from: {chosen_file.filename}")
21
- model_path = hf_hub_download(repo_id, chosen_file.filename)
22
  model = AutoModel.from_pretrained(model_path)
23
  tokenizer = AutoTokenizer.from_pretrained(model_path)
24
  return model, tokenizer
25
 
26
- # Initialize the model
27
- model, tokenizer = load_q2_k_model(model_repo)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from huggingface_hub import hf_hub_list, hf_hub_download
2
  from transformers import AutoModel, AutoTokenizer
3
+ import gradio as gr
4
 
5
+ # Hugging Face repository
6
  model_repo = "mradermacher/Qwen2-2B-RepleteCoder-DHM-GGUF"
7
 
8
+ # Step 1: List and filter GGUF files with 'Q2_K'
9
  def find_q2_k_files(repo_id):
10
  files = hf_hub_list(repo_id)
11
  q2_k_files = [file for file in files if "Q2_K" in file.filename and file.filename.endswith(".gguf")]
 
13
  raise ValueError(f"No files containing 'Q2_K' found in the repository {repo_id}.")
14
  return q2_k_files
15
 
16
+ # Step 2: Load model and tokenizer
17
+ def load_q2_k_model(repo_id, filename):
18
+ model_path = hf_hub_download(repo_id, filename)
 
 
 
 
19
  model = AutoModel.from_pretrained(model_path)
20
  tokenizer = AutoTokenizer.from_pretrained(model_path)
21
  return model, tokenizer
22
 
23
+ # Step 3: Chatbot logic
24
+ def chat(messages, model, tokenizer):
25
+ # Chat template formatting
26
+ input_text = ""
27
+ for i, message in enumerate(messages):
28
+ if i == 0 and message['role'] != 'system':
29
+ input_text += "<|im_start|>system You are a helpful assistant<|im_end|> "
30
+ input_text += f"<|im_start|>{message['role']} {message['content']}<|im_end|> "
31
+ input_text += "<|im_start|>assistant "
32
+
33
+ inputs = tokenizer(input_text, return_tensors="pt")
34
+ outputs = model.generate(**inputs)
35
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
+ return response
37
+
38
+ # Step 4: Gradio Interface
39
+ def chatbot_interface(selected_file, chat_history):
40
+ # Load model dynamically
41
+ model, tokenizer = load_q2_k_model(model_repo, selected_file)
42
+ response = chat(chat_history, model, tokenizer)
43
+ chat_history.append({"role": "assistant", "content": response})
44
+ return chat_history, chat_history
45
+
46
+ # Step 5: Build the UI
47
+ q2_k_files = find_q2_k_files(model_repo)
48
+ file_options = [file.filename for file in q2_k_files]
49
+
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown("# Hugging Face Q2_K Chatbot")
52
+ model_selector = gr.Dropdown(choices=file_options, label="Select Model File")
53
+ chat_history = gr.State([])
54
+ chatbot = gr.Chatbot()
55
+ user_input = gr.Textbox(label="Your Message")
56
+ send_button = gr.Button("Send")
57
+
58
+ # Update chat history
59
+ def update_chat(history, user_message, selected_file):
60
+ history.append({"role": "user", "content": user_message})
61
+ return chatbot_interface(selected_file, history)
62
+
63
+ send_button.click(
64
+ update_chat,
65
+ inputs=[chat_history, user_input, model_selector],
66
+ outputs=[chatbot, chat_history]
67
+ )
68
+
69
+ demo.launch()