not-lain commited on
Commit
036be1c
1 Parent(s): be3750d

switch to a text generation model

Browse files
Files changed (2) hide show
  1. app.py +85 -13
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,22 +1,94 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
 
3
 
4
- qa_model = pipeline("question-answering", "timpal0l/mdeberta-v3-base-squad2")
 
 
 
 
 
 
5
 
6
- description="""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  A resume question-answering interface where a recruter can ask the user about their achievements and skills without the need to interact with them directly or the need to read a really long resume
8
 
9
- **LIMITATIONS:** the bot can only extract specific information and does not take into account multiple sentences at once.
10
  """
11
- examples = ["what's your name?", "what's your email adress ?", "what did you study ?","are you open for work?","what are your skills ?","what's your most recent experience ?"]
12
-
13
- with open("context.txt","r") as f:
14
- # read content from the resume
15
- context = f.read()
 
 
 
16
 
17
- def chat(question,history):
18
- """chat with the QA pipeline"""
19
- return qa_model(question = question, context = context)["answer"].strip()
20
 
21
- demo = gr.ChatInterface(fn=chat, examples=examples, title="Resume QA",description=description,autofocus=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
+ import os
5
+ from threading import Thread
6
+ import spaces
7
 
8
+ token = os.environ["HF_TOKEN"]
9
+ model = AutoModelForCausalLM.from_pretrained(
10
+ "google/gemma-7b-it", torch_dtype=torch.float16, token=token
11
+ )
12
+ tok = AutoTokenizer.from_pretrained("google/gemma-7b-it", token=token)
13
+ device = torch.device("cuda")
14
+ model = model.to(device)
15
 
16
+ with open("context.txt", "r") as f:
17
+ # read content from the resume
18
+ context = f.read()
19
+
20
+
21
+ def format_prompt(message):
22
+ prompt = f"""your name is hafedh hichri and given the following prompt:
23
+ {message}
24
+ you will reply directly without any extra info to the previous prompt given the following context:
25
+ {context}"""
26
+ return prompt
27
+
28
+
29
+ @spaces.GPU
30
+ def chat(message, history):
31
+ chat = []
32
+ for item in history:
33
+ chat.append({"role": "user", "content": item[0]})
34
+ if item[1] is not None:
35
+ chat.append({"role": "assistant", "content": item[1]})
36
+ chat.append({"role": "user", "content": format_prompt(message)})
37
+ messages = tok.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
38
+ # Tokenize the messages string
39
+ model_inputs = tok([messages], return_tensors="pt").to(device)
40
+ streamer = TextIteratorStreamer(
41
+ tok, timeout=10.0, skip_prompt=True, skip_special_tokens=True
42
+ )
43
+ generate_kwargs = dict(
44
+ model_inputs,
45
+ streamer=streamer,
46
+ max_new_tokens=1024,
47
+ do_sample=True,
48
+ top_p=0.95,
49
+ top_k=1000,
50
+ temperature=0.75,
51
+ num_beams=1,
52
+ )
53
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
54
+ t.start()
55
+
56
+ # Initialize an empty string to store the generated text
57
+ partial_text = ""
58
+ for new_text in streamer:
59
+ # print(new_text)
60
+ partial_text += new_text
61
+ # Yield an empty string to cleanup the message textbox and the updated conversation history
62
+ yield partial_text
63
+
64
+
65
+ description = """
66
  A resume question-answering interface where a recruter can ask the user about their achievements and skills without the need to interact with them directly or the need to read a really long resume
67
 
 
68
  """
69
+ examples = [
70
+ "what's your name?",
71
+ "what's your email adress ?",
72
+ "what did you study ?",
73
+ "are you open for work?",
74
+ "what are your skills ?",
75
+ "what's your most recent experience ?",
76
+ ]
77
 
 
 
 
78
 
79
+ demo = gr.ChatInterface(
80
+ fn=chat,
81
+ chatbot=gr.Chatbot(
82
+ show_label=True,
83
+ show_share_button=True,
84
+ show_copy_button=True,
85
+ likeable=True,
86
+ layout="bubble",
87
+ bubble_full_width=False,
88
+ ),
89
+ examples=examples,
90
+ title="Resume QA",
91
+ description=description,
92
+ autofocus=False,
93
+ )
94
  demo.launch()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  transformers
2
- torch
 
 
1
  transformers
2
+ torch==2.2.0
3
+ spaces