Kolumbus Lindh commited on
Commit
377072f
·
1 Parent(s): 243fd65
Files changed (2) hide show
  1. app.py +41 -29
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,12 +1,24 @@
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]],
@@ -15,34 +27,34 @@ 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 +71,6 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
+ from llama_cpp import Llama
3
+ from huggingface_hub import hf_hub_download
4
 
5
+ # Define a function to load the model from the Hugging Face Hub
6
+ def load_model():
7
+ repo_id = "forestav/gguf_lora_model" # Your Hugging Face repo
8
+ model_file = "unsloth.F16.gguf" # Model file in GGUF format
9
 
10
+ # Download the model file
11
+ local_path = hf_hub_download(repo_id=repo_id, filename=model_file)
12
+ print(f"Model loaded from: {local_path}")
13
 
14
+ # Load the model using llama_cpp
15
+ model = Llama(model_path=local_path, n_ctx=2048, n_threads=8, use_metal=False)
16
+ return model
17
+
18
+ # Initialize the model
19
+ model = load_model()
20
+
21
+ # Define the response function for chat interaction
22
  def respond(
23
  message,
24
  history: list[tuple[str, str]],
 
27
  temperature,
28
  top_p,
29
  ):
30
+ try:
31
+ # Prepare the system message and chat history
32
+ messages = [{"role": "system", "content": system_message}]
 
 
 
 
33
 
34
+ # Add the history of the conversation
35
+ for val in history:
36
+ if val[0]:
37
+ messages.append({"role": "user", "content": val[0]})
38
+ if val[1]:
39
+ messages.append({"role": "assistant", "content": val[1]})
40
 
41
+ # Add the current message from the user
42
+ messages.append({"role": "user", "content": message})
43
 
44
+ # Make the model prediction
45
+ response = model.create_chat_completion(
46
+ messages=messages,
47
+ max_tokens=max_tokens,
48
+ temperature=temperature,
49
+ top_p=top_p,
50
+ )
51
+ return response["choices"][0]["message"]["content"]
52
 
53
+ except Exception as e:
54
+ # Return error message if something goes wrong
55
+ return f"Error: {e}"
56
 
57
+ # Define the Gradio interface
 
 
 
58
  demo = gr.ChatInterface(
59
  respond,
60
  additional_inputs=[
 
71
  ],
72
  )
73
 
74
+ # Launch the app
75
  if __name__ == "__main__":
76
  demo.launch()
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- huggingface_hub==0.25.2
 
 
1
+ huggingface_hub==0.25.2
2
+ llama-cpp-python