LLMproj1 commited on
Commit
97731ff
1 Parent(s): f38bb47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -32
app.py CHANGED
@@ -1,48 +1,70 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
4
 
 
5
  """
6
  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
7
  """
8
- client = InferenceClient("LLMproj1/mypersona-llama3-8b")
 
 
 
 
 
9
 
10
 
11
- def respond(
12
- message,
13
- history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- ):
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
 
27
- messages.append({"role": "user", "content": message})
28
-
29
- response = ""
30
-
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
  temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
39
-
40
- response += token
41
- yield response
 
 
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=[
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ import os
5
+ import spaces
6
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
8
+ from threading import Thread
9
 
10
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
11
  """
12
  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
13
  """
14
+ tokenizer = AutoTokenizer.from_pretrained("LLMproj1/mypersona-llama3-8b")
15
+ model = AutoModelForCausalLM.from_pretrained("LLMproj1/mypersona-llama3-8b", device_map="auto")
16
+ terminators = [
17
+ tokenizer.eos_token_id,
18
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
19
+ ]
20
 
21
 
22
+ @spaces.GPU(duration=120)
23
+ def chat_mistral7b_v0dot3(message: str,
24
+ history: list,
25
+ temperature: float,
26
+ max_new_tokens: int
27
+ ) -> str:
28
+ """
29
+ Generate a streaming response using the mistralai/Mistral-7B-Instruct-v0.3 model.
30
+ Args:
31
+ message (str): The input message.
32
+ history (list): The conversation history used by ChatInterface.
33
+ temperature (float): The temperature for generating the response.
34
+ max_new_tokens (int): The maximum number of new tokens to generate.
35
+ Returns:
36
+ str: The generated response.
37
+ """
38
+ conversation = []
39
+ for user, assistant in history:
40
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
41
+ conversation.append({"role": "user", "content": message})
42
 
43
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
44
+
45
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
 
 
46
 
47
+ generate_kwargs = dict(
48
+ input_ids= input_ids,
49
+ streamer=streamer,
50
+ max_new_tokens=max_new_tokens,
51
+ do_sample=True,
 
 
 
52
  temperature=temperature,
53
+ eos_token_id=terminators,
54
+ )
55
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
56
+ if temperature == 0:
57
+ generate_kwargs['do_sample'] = False
58
+
59
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
60
+ t.start()
61
 
62
+ outputs = []
63
+ for text in streamer:
64
+ outputs.append(text)
65
+ #print(outputs)
66
+ yield "".join(outputs)
67
+
68
  demo = gr.ChatInterface(
69
  respond,
70
  additional_inputs=[