aaliyaan commited on
Commit
8e764b1
·
verified ·
1 Parent(s): 0a2d5d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -62
app.py CHANGED
@@ -1,64 +1,155 @@
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
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  demo.launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+
4
+ # Load model and tokenizer
5
+ tokenizer = AutoTokenizer.from_pretrained("aaliyaan/t5-small-finetuned-career")
6
+ model = AutoModelForSeq2SeqLM.from_pretrained("aaliyaan/t5-small-finetuned-career")
7
+
8
+ # Function to generate a response from the model and maintain chat history
9
+ def chat_with_gpt(user_input, chat_history):
10
+ # Tokenize the user input and generate response
11
+ inputs = tokenizer(user_input, return_tensors="pt", padding=True, truncation=True, max_length=512)
12
+ outputs = model.generate(**inputs, max_length=150, num_beams=5, early_stopping=True)
13
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
14
+
15
+ # Update chat history with the new user input and response
16
+ chat_history += f"You: {user_input}\nJobMatch AI: {response}\n"
17
+
18
+ # Return updated chat history and clear input box
19
+ return chat_history, ""
20
+
21
+ # Define the Gradio interface
22
+ def launch_gradio_interface():
23
+ # Create the Gradio Blocks for the layout
24
+ with gr.Blocks() as demo:
25
+ # Title and description section
26
+ gr.Markdown("<h1 style='color: #3A9FD6; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; font-weight: bold; text-align: center;'>JobMatch AI</h1>")
27
+ gr.Markdown("<p style='font-size: 18px; color: #333; text-align: center;'>Your trusted career assistant. Ask any job-related question, and get expert advice!</p>")
28
+
29
+ # Chat history display (above the input box)
30
+ chat_history = gr.Textbox(
31
+ label="Chat History",
32
+ interactive=False,
33
+ show_label=False,
34
+ placeholder="Chat history will appear here...",
35
+ lines=10,
36
+ max_lines=20,
37
+ elem_id="chat-history",
38
+ value="", # Initial empty chat history
39
+ container=False # To avoid any extra padding around this area
40
+ )
41
+
42
+ # User input and output textboxes
43
+ with gr.Row():
44
+ with gr.Column():
45
+ user_input = gr.Textbox(
46
+ label="Ask JobMatch AI",
47
+ placeholder="Type your job-related question here...",
48
+ lines=2,
49
+ max_lines=4,
50
+ interactive=True,
51
+ elem_id="user-input-textbox",
52
+ show_label=False,
53
+ container=False
54
+ )
55
+
56
+ # Button to submit input
57
+ submit_button = gr.Button("Submit", variant="primary")
58
+
59
+ # Link the button with the function (updates chat history and clears input)
60
+ submit_button.click(fn=chat_with_gpt, inputs=[user_input, chat_history], outputs=[chat_history, user_input])
61
+
62
+ # Custom CSS styling
63
+ demo.css = """
64
+ /* Body and Container Styling */
65
+ .gradio-container {
66
+ background: linear-gradient(135deg, #FF9A8B, #FF6A00); /* Gradient background with warm colors */
67
+ border-radius: 15px;
68
+ box-shadow: 0px 6px 30px rgba(0, 0, 0, 0.1); /* Soft shadow for depth */
69
+ padding: 50px;
70
+ width: 70%; /* Adjust width for centering */
71
+ margin: 0 auto; /* Centering the container */
72
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* Professional font */
73
+ }
74
+
75
+ /* Title Styling */
76
+ .gradio-title {
77
+ font-size: 36px;
78
+ font-weight: 700;
79
+ color: #ffffff; /* White color for contrast */
80
+ text-align: center;
81
+ margin-bottom: 20px;
82
+ }
83
+
84
+ /* Description Styling */
85
+ .gradio-description {
86
+ font-size: 18px;
87
+ color: #eeeeee; /* Light gray text for description */
88
+ text-align: center;
89
+ margin-bottom: 30px;
90
+ }
91
+
92
+ /* Chat History Box Styling */
93
+ #chat-history {
94
+ font-size: 16px;
95
+ border-radius: 12px;
96
+ border: none;
97
+ padding: 14px;
98
+ background: rgba(255, 255, 255, 0.9); /* Slight transparent white background */
99
+ color: #444;
100
+ margin-bottom: 20px;
101
+ height: 300px;
102
+ overflow-y: auto;
103
+ font-family: 'Roboto', sans-serif;
104
+ box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1); /* Soft shadow for chat history */
105
+ }
106
+
107
+ /* Input Textbox Styling */
108
+ #user-input-textbox {
109
+ font-size: 16px;
110
+ border-radius: 10px;
111
+ border: none;
112
+ padding: 12px;
113
+ width: 100%;
114
+ margin-bottom: 15px;
115
+ background-color: #FFFFFF;
116
+ font-family: 'Roboto', sans-serif;
117
+ transition: background-color 0.3s ease; /* Smooth transition for hover effect */
118
+ }
119
+
120
+ #user-input-textbox:focus {
121
+ background-color: #FFFFFF; /* Light gray background on focus */
122
+ border-color: #FF6A00; /* Orange border on focus */
123
+ }
124
+
125
+ /* Submit Button Styling */
126
+ .gradio-button {
127
+ background-color: #3A9FD6; /* Blue color for submit button */
128
+ color: white;
129
+ padding: 14px 28px;
130
+ border-radius: 8px;
131
+ box-shadow: 0px 6px 30px H000000; /* Soft shadow for depth */
132
+ border: none;
133
+ font-size: 18px;
134
+ font-weight: 600;
135
+ cursor: pointer;
136
+ transition: all 0.3s ease-in-out;
137
+ }
138
+
139
+ .gradio-button:hover {
140
+ background-color: #FF6A00; /* Orange color on hover */
141
+ transform: scale(1.05); /* Slight button scaling on hover */
142
+ }
143
+
144
+ /* Adjust padding and margins */
145
+ .gradio-block {
146
+ padding: 0;
147
+ }
148
+ .gradio-row {
149
+ margin-bottom: 25px;
150
+ }
151
+ """
152
+
153
  demo.launch()
154
+
155
+