aaliyaan commited on
Commit
c8ccaac
·
1 Parent(s): 1775594
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -1,20 +1,17 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM
3
  from PyPDF2 import PdfReader
4
 
5
- # Models and Tokenizers Setup
6
  models = {
7
- "Text Generator (Bloom)": {
8
- "model": AutoModelForCausalLM.from_pretrained("bigscience/bloom-560m"),
9
- "tokenizer": AutoTokenizer.from_pretrained("bigscience/bloom-560m"),
10
  },
11
  "PDF Summarizer (T5)": {
12
- "model": AutoModelForSeq2SeqLM.from_pretrained("aaliyaan/t5-small-finetuned-career"),
13
- "tokenizer": AutoTokenizer.from_pretrained("aaliyaan/t5-small-finetuned-career"),
14
  },
15
  "Broken Answer (T0pp)": {
16
- "model": AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp"),
17
- "tokenizer": AutoTokenizer.from_pretrained("bigscience/T0pp"),
18
  },
19
  }
20
 
@@ -28,24 +25,25 @@ def chat_with_model(model_choice, user_message, chat_history, file=None):
28
  return chat_history
29
 
30
  model_info = models[model_choice]
31
- tokenizer = model_info["tokenizer"]
32
- model = model_info["model"]
33
-
34
- # Tokenize Input
35
- inputs = tokenizer(user_message, return_tensors="pt", padding=True, truncation=True, max_length=512)
36
-
37
- # Adjust max_length and parameters for the PDF summarizer
38
- max_length = 150
39
- num_beams = 5
40
- outputs = model.generate(
41
- **inputs,
42
- max_length=max_length,
43
- num_beams=num_beams,
44
- early_stopping=True,
45
- no_repeat_ngram_size=2
46
- )
47
-
48
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
49
 
50
  # Update Chat History
51
  chat_history.append((user_message, response))
@@ -79,7 +77,7 @@ def create_chat_interface():
79
  with gr.Row():
80
  model_choice = gr.Dropdown(
81
  choices=list(models.keys()),
82
- value="Text Generator (Bloom)",
83
  label="Select Model"
84
  )
85
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
  from PyPDF2 import PdfReader
4
 
5
+ # Models Setup
6
  models = {
7
+ "Text Generator (Zephyr)": {
8
+ "client": InferenceClient(model="HuggingFaceH4/zephyr-7b-beta"),
 
9
  },
10
  "PDF Summarizer (T5)": {
11
+ "client": InferenceClient(model="aaliyaan/t5-small-finetuned-career"),
 
12
  },
13
  "Broken Answer (T0pp)": {
14
+ "client": InferenceClient(model="bigscience/T0pp"),
 
15
  },
16
  }
17
 
 
25
  return chat_history
26
 
27
  model_info = models[model_choice]
28
+ client = model_info["client"]
29
+
30
+ # Prepare messages for the InferenceClient
31
+ messages = [
32
+ {"role": "system", "content": "You are a helpful assistant."},
33
+ {"role": "user", "content": user_message}
34
+ ]
35
+
36
+ # Generate Response
37
+ response = ""
38
+ for message in client.chat_completion(
39
+ messages,
40
+ max_tokens=150,
41
+ stream=True,
42
+ temperature=0.7,
43
+ top_p=0.95
44
+ ):
45
+ token = message.choices[0].delta.content
46
+ response += token
47
 
48
  # Update Chat History
49
  chat_history.append((user_message, response))
 
77
  with gr.Row():
78
  model_choice = gr.Dropdown(
79
  choices=list(models.keys()),
80
+ value="Text Generator (Zephyr)",
81
  label="Select Model"
82
  )
83