Anshul4150 commited on
Commit
6678a75
·
verified ·
1 Parent(s): eb0f5f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -1,20 +1,22 @@
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:
@@ -25,24 +27,23 @@ def respond(
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 +60,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
+ # Model ID
6
+ model_id = "apu20/Llama-3.2-3B-Instruct_Tele"
 
 
7
 
8
+ # Load quantized model (switch to 8-bit if needed)
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ model_id,
11
+ torch_dtype=torch.float16, # Use float16 for reduced memory footprint
12
+ device_map="cpu" # Force model to run on CPU
13
+ )
14
+
15
+ # Load tokenizer
16
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
17
 
18
+
19
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
 
 
 
 
 
 
20
  messages = [{"role": "system", "content": system_message}]
21
 
22
  for val in history:
 
27
 
28
  messages.append({"role": "user", "content": message})
29
 
30
+ # Tokenize input
31
+ inputs = tokenizer(message, return_tensors="pt").to("cpu") # Ensure inputs are on CPU
32
 
33
+ # Generate response
34
+ with torch.no_grad():
35
+ outputs = model.generate(
36
+ **inputs,
37
+ max_length=max_tokens,
38
+ temperature=temperature,
39
+ top_p=top_p
40
+ )
41
 
42
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
43
+ return response
44
 
45
 
46
+ # Gradio Chat Interface
 
 
47
  demo = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
 
60
  ],
61
  )
62
 
 
63
  if __name__ == "__main__":
64
  demo.launch()