Komal-patra commited on
Commit
b0e417e
1 Parent(s): 8bf2568
Files changed (1) hide show
  1. app.py +29 -38
app.py CHANGED
@@ -6,11 +6,9 @@ from peft import PeftModel, PeftConfig
6
 
7
  # Hugging Face login
8
  token = os.environ.get("token")
9
- if token:
10
- login(token)
11
- print("Login is successful")
12
- else:
13
- print("Token not found. Please set your token in the environment variables.")
14
 
15
  # Model and tokenizer setup
16
  MODEL_NAME = "google/flan-t5-base"
@@ -20,7 +18,7 @@ base_model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
20
  model = PeftModel.from_pretrained(base_model, "Komal-patra/results")
21
 
22
  # Text generation function
23
- def generate_text(prompt, max_length=150):
24
  inputs = tokenizer(prompt, return_tensors="pt")
25
  outputs = model.generate(
26
  input_ids=inputs["input_ids"],
@@ -34,34 +32,31 @@ def generate_text(prompt, max_length=150):
34
  # Custom CSS for the UI
35
  background_image_path = 'https://www.shlegal-technology.com/sites/default/files/insight/ExploringTheLegislativeBackgroundBANNER.jpg'
36
  custom_css = f"""
37
- /* Chatbot container */
38
- .gradio-container {{
39
- color: white; /* Light text color */
40
- background-image: url('{background_image_path}'); /* Add background image */
41
- background-size: cover; /* Cover the entire container */
42
- background-position: center; /* Center the image */
43
- background-repeat: no-repeat; /* Do not repeat the image */
44
- background-color: rgba(0, 0, 0, 0.5); /* Semi-transparent dark background */
45
- }}
46
-
47
- /* Transparent message bubbles */
48
  .message.pending {{
49
- background: rgba(168, 196, 214, 0.1);
50
  }}
 
51
  .message.bot.svelte-1s78gfg.message-bubble-border {{
52
- border-color: rgba(38, 107, 153, 0.1);
53
- background: rgba(255, 255, 255, 0.1);
54
  }}
 
55
  .message.user.svelte-1s78gfg.message-bubble-border {{
56
- background: rgba(157, 221, 249, 0.1);
57
- border-color: rgba(157, 221, 249, 0.1);
58
  }}
59
-
60
  /* For both user and response message as per the document */
61
  span.md.svelte-8tpqd2.chatbot.prose p {{
62
  color: #266B99;
63
  }}
64
-
 
 
 
 
 
 
 
 
65
  /* RED (Hex: #DB1616) for action buttons and links only */
66
  .clear-btn {{
67
  background: #DB1616;
@@ -89,39 +84,35 @@ span.md.svelte-8tpqd2.chatbot.prose p {{
89
  content: url('file=Komal-patra/EU_AI_ACT/orcawise image.png');
90
  margin-right: 8px;
91
  }}
92
-
93
  /* Enable scrolling for the chatbot messages */
94
  .chatbot .messages {{
95
  max-height: 500px; /* Adjust as needed */
96
  overflow-y: auto;
97
  }}
98
-
99
- /* Transparent overall message container */
100
- .gradio-chatbot .wrap {{
101
- background: transparent; /* Make the container background transparent */
102
- border: none; /* Remove any borders if necessary */
103
- }}
104
  """
105
 
106
  # Gradio interface setup
107
  with gr.Blocks(css=custom_css) as demo:
 
108
  chatbot = gr.Chatbot()
109
- msg = gr.Textbox(placeholder="Ask your question...", show_label=False)
110
  submit_button = gr.Button("Submit", elem_classes="submit-btn")
111
  clear = gr.Button("Clear", elem_classes="clear-btn")
112
 
 
113
  def user(user_message, history):
114
  return "", history + [[user_message, None]]
115
 
 
116
  def bot(history):
117
- if len(history) == 1:
118
  bot_message = "Hi there! How can I help you today?"
119
- history[-1][1] = bot_message
120
  else:
121
- history[-1][1] = ""
122
- previous_message = history[-1][0]
123
- bot_message = generate_text(previous_message)
124
- history[-1][1] = bot_message
125
  return history
126
 
127
  submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
 
6
 
7
  # Hugging Face login
8
  token = os.environ.get("token")
9
+ login(token)
10
+ print("login is succesful")
11
+ max_length=512
 
 
12
 
13
  # Model and tokenizer setup
14
  MODEL_NAME = "google/flan-t5-base"
 
18
  model = PeftModel.from_pretrained(base_model, "Komal-patra/results")
19
 
20
  # Text generation function
21
+ def generate_text(prompt, max_length=512):
22
  inputs = tokenizer(prompt, return_tensors="pt")
23
  outputs = model.generate(
24
  input_ids=inputs["input_ids"],
 
32
  # Custom CSS for the UI
33
  background_image_path = 'https://www.shlegal-technology.com/sites/default/files/insight/ExploringTheLegislativeBackgroundBANNER.jpg'
34
  custom_css = f"""
 
 
 
 
 
 
 
 
 
 
 
35
  .message.pending {{
36
+ background: #A8C4D6;
37
  }}
38
+ /* Response message */
39
  .message.bot.svelte-1s78gfg.message-bubble-border {{
40
+ border-color: #266B99;
 
41
  }}
42
+ /* User message */
43
  .message.user.svelte-1s78gfg.message-bubble-border {{
44
+ background: #9DDDF9;
45
+ border-color: #9DDDF9;
46
  }}
 
47
  /* For both user and response message as per the document */
48
  span.md.svelte-8tpqd2.chatbot.prose p {{
49
  color: #266B99;
50
  }}
51
+ /* Chatbot container */
52
+ .gradio-container {{
53
+ background: #1c1c1c; /* Dark background */
54
+ color: white; /* Light text color */
55
+ background-image: url('{background_image_path}'); /* Add background image */
56
+ background-size: cover; /* Cover the entire container */
57
+ background-position: center; /* Center the image */
58
+ background-repeat: no-repeat; /* Do not repeat the image */
59
+ }}
60
  /* RED (Hex: #DB1616) for action buttons and links only */
61
  .clear-btn {{
62
  background: #DB1616;
 
84
  content: url('file=Komal-patra/EU_AI_ACT/orcawise image.png');
85
  margin-right: 8px;
86
  }}
 
87
  /* Enable scrolling for the chatbot messages */
88
  .chatbot .messages {{
89
  max-height: 500px; /* Adjust as needed */
90
  overflow-y: auto;
91
  }}
 
 
 
 
 
 
92
  """
93
 
94
  # Gradio interface setup
95
  with gr.Blocks(css=custom_css) as demo:
96
+ gr.Markdown("<h1>Ask a question about the EU AI Act</h1>")
97
  chatbot = gr.Chatbot()
98
+ msg = gr.Textbox(placeholder="Ask your question...", show_label=False) # Add placeholder text
99
  submit_button = gr.Button("Submit", elem_classes="submit-btn")
100
  clear = gr.Button("Clear", elem_classes="clear-btn")
101
 
102
+ # Function to handle user input
103
  def user(user_message, history):
104
  return "", history + [[user_message, None]]
105
 
106
+ # Function to handle bot response
107
  def bot(history):
108
+ if len(history) == 1: # Check if it's the first interaction
109
  bot_message = "Hi there! How can I help you today?"
110
+ history[-1][1] = bot_message # Add welcome message to history
111
  else:
112
+ history[-1][1] = "" # Clear the last bot message
113
+ previous_message = history[-1][0] # Access the previous user message
114
+ bot_message = generate_text(previous_message) # Generate response based on previous message
115
+ history[-1][1] = bot_message # Update the last bot message
116
  return history
117
 
118
  submit_button.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(