Komal-patra commited on
Commit
b4ceb72
1 Parent(s): f2c165a

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -53
app.py CHANGED
@@ -1,63 +1,123 @@
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:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
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
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import login
3
+ from transformers import AutoModelForSeq2SeqLM, T5Tokenizer
4
+ from peft import PeftModel, PeftConfig
5
 
6
+ token = os.environ.get("token")
7
+ login(token)
8
+ print("login is succesful")
9
+ max_length=512
10
 
11
+ MODEL_NAME = "google/flan-t5-base"
12
+ tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME, token=token)
13
+ config = PeftConfig.from_pretrained("Orcawise/results")
14
+ base_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
15
+ model = PeftModel.from_pretrained(base_model, "Orcawise/results")
16
 
17
+ #gr.Interface.from_pipeline(pipe).launch()
 
 
 
 
 
 
 
 
18
 
19
+ def generate_text(prompt, max_length=512):
20
+ """Generates text using the PEFT model.
21
+ Args:
22
+ prompt (str): The user-provided prompt to start the generation.
23
+ Returns:
24
+ str: The generated text.
25
+ """
26
 
 
27
 
28
+ # Preprocess the prompt
29
+ # inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
30
+ inputs = tokenizer(prompt, return_tensors="pt")
31
 
32
+ # Generate text using beam search
33
+ outputs = model.generate(
34
+ input_ids = inputs["input_ids"],
35
+ max_length=max_length,
36
+ num_beams=1
37
+
38
+ )
 
39
 
40
+ # Decode the generated tokens
41
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
42
+ print("show the generated text", generated_text)
43
+ return generated_text
44
 
45
+ #############
46
+ custom_css="""
47
+ .message.pending {
48
+ background: #A8C4D6;
49
+ }
50
+ /* Response message */
51
+ .message.bot.svelte-1s78gfg.message-bubble-border {
52
+ /* background: white; */
53
+ border-color: #266B99
54
+ }
55
+ /* User message */
56
+ .message.user.svelte-1s78gfg.message-bubble-border{
57
+ background: #9DDDF9;
58
+ border-color: #9DDDF9
59
+
60
+ }
61
+ /* For both user and response message as per the document */
62
+ span.md.svelte-8tpqd2.chatbot.prose p {
63
+ color: #266B99;
64
+ }
65
+ /* Chatbot comtainer */
66
+ .gradio-container{
67
+ /* background: #84D5F7 */
68
+ }
69
+ /* RED (Hex: #DB1616) for action buttons and links only */
70
+ .clear-btn {
71
+ background: #DB1616;
72
+ color: white;
73
+ }
74
+ /* #84D5F7 - Primary colours are set to be used for all sorts */
75
+ .submit-btn {
76
+ background: #266B99;
77
+ color: white;
78
+ }
79
  """
80
+
81
+ ### working correctly but the welcoming message isnt rendering
82
+ with gr.Blocks(css=custom_css) as demo:
83
+ chatbot = gr.Chatbot()
84
+ msg = gr.Textbox(placeholder="Ask your question...") # Add placeholder text
85
+ submit_button = gr.Button("Submit", elem_classes="submit-btn")
86
+ clear = gr.Button("Clear", elem_classes="clear-btn")
87
+
88
+
89
+ def user(user_message, history):
90
+ return "", history + [[user_message, None]]
91
+
92
+
93
+ def bot(history):
94
+ history[-1][1] = "" # Update the last bot message (welcome message or response)
95
+ if len(history) < 0: # Check if it's the first interaction
96
+ bot_message = "Hi there! How can I help you today?"
97
+ history.append([None, bot_message]) # Add welcome message to history
98
+ for character in bot_message:
99
+ history[-1][1] += character
100
+ yield history # Yield the updated history character by character
101
+
102
+ else:
103
+ previous_message = history[-1][0] # Access the previous user message
104
+ bot_message = generate_text(previous_message) # Generate response based on previous message
105
+ for character in bot_message:
106
+ history[-1][1] += character
107
+ yield history # Yield the updated history character by character
108
+
109
+
110
+
111
+ # Connect submit button to user and then bot functions
112
+ submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
113
+ bot, chatbot, chatbot
114
+ )
115
+
116
+ # Trigger user function on Enter key press (same chain as submit button)
117
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
118
+ bot, chatbot, chatbot
119
+ )
120
+
121
+ clear.click(lambda: None, None, chatbot, queue=False)
122
+
123
+ demo.launch()