aaliyaan commited on
Commit
e8b622f
·
verified ·
1 Parent(s): a73dd24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -154
app.py CHANGED
@@ -1,155 +1,79 @@
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
- launch_gradio_interface()
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import PyPDF2
4
+
5
+ # Load Models
6
+ model1_name = "t5-small" # Model 1
7
+ model2_name = "codeparrot/codeparrot-small" # Model 2
8
+ model3_name = "Salesforce/blip-image-captioning" # Model 3
9
+
10
+ # Load Pipelines
11
+ model1 = pipeline("text2text-generation", model=model1_name, tokenizer=model1_name)
12
+ model2 = pipeline("text-generation", model=model2_name, tokenizer=model2_name)
13
+ model3 = pipeline("image-to-text", model=model3_name) # We'll adapt this for PDF processing
14
+
15
+ # Helper: Extract text from PDF
16
+ def extract_text_from_pdf(pdf_file):
17
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
18
+ text = ""
19
+ for page in pdf_reader.pages:
20
+ text += page.extract_text()
21
+ return text
22
+
23
+ # Function for Model 1
24
+ def model1_func(input_text):
25
+ try:
26
+ result = model1(input_text, max_length=50, num_return_sequences=1)
27
+ answer = result[0]["generated_text"]
28
+ return f"Model 1 Output: {answer}"
29
+ except Exception as e:
30
+ return f"Error: {str(e)}"
31
+
32
+ # Function for Model 2
33
+ def model2_func(input_text):
34
+ try:
35
+ result = model2(input_text, max_length=50, num_return_sequences=1)
36
+ answer = result[0]["generated_text"]
37
+ return f"Model 2 Output: {answer}"
38
+ except Exception as e:
39
+ return f"Error: {str(e)}"
40
+
41
+ # Function for Model 3
42
+ def model3_func(pdf_file):
43
+ try:
44
+ extracted_text = extract_text_from_pdf(pdf_file)
45
+ if not extracted_text.strip():
46
+ return "No text found in the PDF. Please upload a valid file."
47
+ result = model3(extracted_text)
48
+ answer = result[0]["generated_text"]
49
+ return f"Model 3 Output: {answer}"
50
+ except Exception as e:
51
+ return f"Error: {str(e)}"
52
+
53
+ # Gradio Interface
54
+ with gr.Blocks() as demo:
55
+ gr.Markdown("<h1>Multi-Model NLP Tool</h1>")
56
+
57
+ with gr.Tab("Model 1"):
58
+ gr.Markdown("**Model 1: Text-to-Text (e.g., Summarization)**")
59
+ model1_input = gr.Textbox(label="Enter Text", placeholder="Type here...")
60
+ model1_output = gr.Textbox(label="Output")
61
+ model1_button = gr.Button("Generate")
62
+ model1_button.click(model1_func, inputs=model1_input, outputs=model1_output)
63
+
64
+ with gr.Tab("Model 2"):
65
+ gr.Markdown("**Model 2: Text Generation (e.g., Code Generation)**")
66
+ model2_input = gr.Textbox(label="Enter Text", placeholder="Type here...")
67
+ model2_output = gr.Textbox(label="Output")
68
+ model2_button = gr.Button("Generate")
69
+ model2_button.click(model2_func, inputs=model2_input, outputs=model2_output)
70
+
71
+ with gr.Tab("Model 3"):
72
+ gr.Markdown("**Model 3: Document Reader (PDF Input)**")
73
+ pdf_input = gr.File(label="Upload PDF")
74
+ model3_output = gr.Textbox(label="Output")
75
+ model3_button = gr.Button("Process PDF")
76
+ model3_button.click(model3_func, inputs=pdf_input, outputs=model3_output)
77
+
78
+ # Launch the app
79
+ demo.launch()