Ikeofai commited on
Commit
248cc1f
·
verified ·
1 Parent(s): a002823

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -11
app.py CHANGED
@@ -1,12 +1,98 @@
1
- # import pip
2
- # def upgrade_packages():
3
- # try:
4
- # pip.main(['install', '--upgrade', 'transformers','peft']) # Replace with other packages to upgrade
5
- # print("Packages upgraded successfully!")
6
- # except Exception as e:
7
- # print(f"Error upgrading packages: {e}")
8
- # Call the upgrade function
9
- #upgrade_packages()
10
- #checks
11
  import gradio as gr
12
- gr.load("models/Ikeofai/outputs").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
+ from huggingface_hub import login
4
+ token = os.environ.get("token")
5
+ login(token)
6
+ ##########
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM
8
+ from peft import PeftModel, PeftConfig
9
+
10
+ from transformers import AutoTokenizer, pipeline
11
+
12
+ # Assuming you have it stored securely
13
+
14
+ model_name="models/Ikeofai/outputs"
15
+ max_length=200
16
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
17
+ config = PeftConfig.from_pretrained(model_name,token=token)
18
+ model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
19
+ model = PeftModel.from_pretrained(model, model_name)
20
+
21
+ #testing
22
+
23
+ #tokenizer = AutoTokenizer.from_pretrained("Orcawise/eu-ai-act-align", use_fast=True,max_length=200)
24
+ #pipe = pipeline("text2text-generation", model=model,tokenizer=tokenizer)
25
+ #pipe = pipeline("conversational", model="google/vit-base-patch16-224")
26
+
27
+ #gr.Interface.from_pipeline(pipe).launch()
28
+
29
+ def generate_text(prompt):
30
+ """Generates text using the PEFT model.
31
+ Args:
32
+ prompt (str): The user-provided prompt to start the generation.
33
+ Returns:
34
+ str: The generated text.
35
+ """
36
+
37
+
38
+ # Preprocess the prompt
39
+ input_ids = tokenizer(prompt, return_tensors="pt")["input_ids"]
40
+
41
+ # Generate text using beam search
42
+ output = model.generate(
43
+ input_ids=input_ids,
44
+ max_length=max_length,
45
+ num_beams=1, # Adjust num_beams for better quality (may increase processing time)
46
+ )
47
+
48
+ # Decode the generated tokens
49
+ generated_text = tokenizer.batch_decode(output, skip_special_tokens=True)[0]
50
+
51
+
52
+ return generated_text
53
+ #############
54
+
55
+
56
+ ### working correctly but the welcoming message isnt rendering
57
+ with gr.Blocks() as demo:
58
+ chatbot = gr.Chatbot()
59
+ msg = gr.Textbox(placeholder="Ask your question...") # Add placeholder text
60
+ submit_button = gr.Button("Submit")
61
+ clear = gr.Button("Clear")
62
+
63
+
64
+ def user(user_message, history):
65
+ return "", history + [[user_message, None]]
66
+
67
+
68
+ def bot(history):
69
+ history[-1][1] = "" # Update the last bot message (welcome message or response)
70
+ if len(history) < 0: # Check if it's the first interaction
71
+ bot_message = "Hi there! How can I help you today?"
72
+ history.append([None, bot_message]) # Add welcome message to history
73
+ for character in bot_message:
74
+ history[-1][1] += character
75
+ yield history # Yield the updated history character by character
76
+
77
+ else:
78
+ previous_message = history[-1][0] # Access the previous user message
79
+ bot_message = generate_text(previous_message) # Generate response based on previous message
80
+ for character in bot_message:
81
+ history[-1][1] += character
82
+ yield history # Yield the updated history character by character
83
+
84
+
85
+
86
+ # Connect submit button to user and then bot functions
87
+ submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
88
+ bot, chatbot, chatbot
89
+ )
90
+
91
+ # Trigger user function on Enter key press (same chain as submit button)
92
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
93
+ bot, chatbot, chatbot
94
+ )
95
+
96
+ clear.click(lambda: None, None, chatbot, queue=False)
97
+
98
+ demo.launch()