ConceptModels commited on
Commit
d96cffe
·
verified ·
1 Parent(s): e1b7471

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -29
app.py CHANGED
@@ -1,6 +1,37 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def respond(
6
  message,
@@ -9,45 +40,49 @@ def respond(
9
  max_tokens,
10
  temperature,
11
  top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="ConceptModels/Concept-7b-V1-Full")
18
-
19
  messages = [{"role": "system", "content": system_message}]
20
-
21
  messages.extend(history)
22
-
23
  messages.append({"role": "user", "content": message})
24
 
25
- response = ""
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
 
 
 
 
31
  temperature=temperature,
32
  top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = 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
  chatbot = gr.ChatInterface(
47
  respond,
48
  type="messages",
49
  additional_inputs=[
50
- gr.Textbox(value="You are an AI called Concept. Your made for programming in any type of code.", 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(
@@ -61,10 +96,9 @@ chatbot = gr.ChatInterface(
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, TextIteratorStreamer
4
+ from threading import Thread
5
 
6
+ # 1. Configuration
7
+ MODEL_ID = "ConceptModels/Concept-7b-V1-Full"
8
+
9
+ # 2. Load Model and Tokenizer (Done once at startup)
10
+ print(f"Loading {MODEL_ID}... this may take a while.")
11
+
12
+ try:
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
+
15
+ # Attempt to use GPU if available, otherwise CPU
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ print(f"Running on device: {device}")
18
+
19
+ model = AutoModelForCausalLM.from_pretrained(
20
+ MODEL_ID,
21
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
22
+ device_map="auto" if device == "cuda" else None,
23
+ # Uncomment the line below to use 4-bit quantization (requires pip install bitsandbytes)
24
+ # load_in_4bit=True
25
+ )
26
+ # If using CPU, move model explicitly
27
+ if device == "cpu":
28
+ model.to("cpu")
29
+
30
+ print("Model loaded successfully.")
31
+
32
+ except Exception as e:
33
+ print(f"Error loading model: {e}")
34
+ raise e
35
 
36
  def respond(
37
  message,
 
40
  max_tokens,
41
  temperature,
42
  top_p,
43
+ hf_token=None, # Not strictly needed for local if logged in via CLI, but kept for signature compatibility
44
  ):
45
+ # 3. Format the conversation
46
+ # We construct the list of messages including system, history, and current input
 
 
 
47
  messages = [{"role": "system", "content": system_message}]
 
48
  messages.extend(history)
 
49
  messages.append({"role": "user", "content": message})
50
 
51
+ # Apply the model's specific chat template
52
+ input_ids = tokenizer.apply_chat_template(
53
+ messages,
54
+ return_tensors="pt",
55
+ add_generation_prompt=True
56
+ ).to(model.device)
57
 
58
+ # 4. Setup Streaming
59
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
60
+
61
+ generate_kwargs = dict(
62
+ input_ids=input_ids,
63
+ streamer=streamer,
64
+ max_new_tokens=max_tokens,
65
+ do_sample=True,
66
  temperature=temperature,
67
  top_p=top_p,
68
+ )
 
 
 
 
69
 
70
+ # 5. Run generation in a separate thread so we can yield tokens
71
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
72
+ t.start()
73
 
74
+ # 6. Yield output as it generates
75
+ partial_message = ""
76
+ for new_token in streamer:
77
+ partial_message += new_token
78
+ yield partial_message
79
 
80
+ # 7. Gradio Interface
 
 
81
  chatbot = gr.ChatInterface(
82
  respond,
83
  type="messages",
84
  additional_inputs=[
85
+ gr.Textbox(value="You are an AI called Concept. You are made for programming in any type of code.", label="System message"),
86
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
87
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
88
  gr.Slider(
 
96
  )
97
 
98
  with gr.Blocks() as demo:
99
+ # Removed LoginButton because local execution usually relies on environment login
100
+ # or public models.
101
  chatbot.render()
102
 
 
103
  if __name__ == "__main__":
104
+ demo.launch()