import gradio as gr import os from huggingface_hub import login from transformers import AutoModelForSeq2SeqLM, T5Tokenizer from peft import PeftModel, PeftConfig token = os.environ.get("token") login(token) print("login is succesful") max_length=512 MODEL_NAME = "google/flan-t5-base" tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME, token=token) config = PeftConfig.from_pretrained("Orcawise/eu_ai_act_orcawise_july12") base_model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base") model = PeftModel.from_pretrained(base_model, "Orcawise/eu_ai_act_orcawise_july12") #gr.Interface.from_pipeline(pipe).launch() def generate_text(prompt): """Generates text using the PEFT model. Args: prompt (str): The user-provided prompt to start the generation. Returns: str: The generated text. """ # Preprocess the prompt # inputs = tokenizer(prompt, return_tensors="pt").to("cuda") inputs = tokenizer(prompt, return_tensors="pt") # Generate text using beam search outputs = model.generate( input_ids = inputs["input_ids"], max_length=max_length, num_beams=3, repetition_penalty=2.2, ) print("show the output", outputs) # Decode the generated tokens generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) print("show the generated text", generated_text) return generated_text ############# custom_css=""" .message.pending { background: #A8C4D6; } /* Response message */ .message.bot.svelte-1s78gfg.message-bubble-border { /* background: white; */ border-color: #266B99 } /* User message */ .message.user.svelte-1s78gfg.message-bubble-border{ background: #9DDDF9; border-color: #9DDDF9 } /* For both user and response message as per the document */ span.md.svelte-8tpqd2.chatbot.prose p { color: #266B99; } /* Chatbot comtainer */ .gradio-container{ /* background: #84D5F7 */ } /* RED (Hex: #DB1616) for action buttons and links only */ .clear-btn { background: #DB1616; color: white; } /* #84D5F7 - Primary colours are set to be used for all sorts */ .submit-btn { background: #266B99; color: white; } """ ### working correctly but the welcoming message isnt rendering with gr.Blocks(css=custom_css) as demo: chatbot = gr.Chatbot() msg = gr.Textbox(placeholder="Ask your question...") # Add placeholder text submit_button = gr.Button("Submit", elem_classes="submit-btn") clear = gr.Button("Clear", elem_classes="clear-btn") def user(user_message, history): return "", history + [[user_message, None]] def bot(history): history[-1][1] = "" # Update the last bot message (welcome message or response) if len(history) < 0: # Check if it's the first interaction bot_message = "Hi there! How can I help you today?" history.append([None, bot_message]) # Add welcome message to history for character in bot_message: history[-1][1] += character yield history # Yield the updated history character by character else: previous_message = history[-1][0] # Access the previous user message bot_message = generate_text(previous_message) # Generate response based on previous message for character in bot_message: history[-1][1] += character yield history # Yield the updated history character by character # Connect submit button to user and then bot functions submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot, chatbot, chatbot ) # Trigger user function on Enter key press (same chain as submit button) msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( bot, chatbot, chatbot ) clear.click(lambda: None, None, chatbot, queue=False) demo.launch()