Reshmarb commited on
Commit
daf8adb
·
1 Parent(s): c6a3095

project added

Browse files
Files changed (2) hide show
  1. app.py +95 -50
  2. requirements.txt +16 -17
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # Import necessary libraries
2
  from groq import Groq
3
  import gradio as gr
4
  from gtts import gTTS
@@ -10,11 +9,11 @@ import logging
10
  import spacy
11
  from transformers import pipeline
12
  import torch
13
- import cv2
14
- import numpy as np
15
  from torchvision import transforms
16
  import pathlib
17
-
 
18
 
19
  # Pathlib adjustment for Windows compatibility
20
  temp = pathlib.PosixPath
@@ -31,60 +30,29 @@ file_handler.setFormatter(formatter)
31
  logger.addHandler(console_handler)
32
  logger.addHandler(file_handler)
33
 
34
- # Initialize Groq Client
35
  client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
36
 
37
- # Initialize Groq Client
38
  #client = Groq(api_key="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")
39
 
40
  # Initialize spaCy NLP model for named entity recognition (NER)
41
- import spacy
42
-
43
- # Download the model if it's not already installed
44
- try:
45
- nlp = spacy.load("en_core_web_sm")
46
- except OSError:
47
- print("Downloading 'en_core_web_sm' model...")
48
- import os
49
- os.system("python -m spacy download en_core_web_sm")
50
- nlp = spacy.load("en_core_web_sm")
51
-
52
- # Your code continues here
53
- print("Model loaded successfully!")
54
 
55
  # Initialize sentiment analysis model using Hugging Face
56
  sentiment_analyzer = pipeline("sentiment-analysis")
57
 
58
- import torch
59
- import os
60
-
61
  def load_yolov5_model():
62
- # Load model from Hugging Face Hub or local path
63
  model = torch.hub.load(
64
- 'ultralytics/yolov5', # Use the official YOLOv5 repo
65
  'custom',
66
- path='models/best.pt', # Relative path to the model file
67
- source='local' # Change to 'github' if loading from the official repo
68
  )
 
69
  return model
70
 
71
- # Example usage
72
- if __name__ == "__main__":
73
- model = load_yolov5_model()
74
- print("Model loaded successfully!")
75
-
76
-
77
- # Load pre-trained YOLOv5 model
78
- # def load_yolov5_model():
79
- # model = torch.hub.load(
80
- # r'C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\yolov5',
81
- # 'custom',
82
- # path=r"C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\models\best.pt",
83
- # source="local"
84
- # )
85
- # model.eval()
86
- # return model
87
-
88
  model = load_yolov5_model()
89
 
90
  # Function to preprocess user input for better NLP understanding
@@ -157,13 +125,9 @@ def predict_image(image):
157
  if image is None:
158
  return "Error: No image uploaded.", "No description available."
159
 
160
- # Convert PIL image to NumPy array
161
  image_np = np.array(image) # Convert PIL image to NumPy array
162
 
163
- # Handle grayscale images
164
- if len(image_np.shape) == 2: # Grayscale image
165
- image_np = cv2.cvtColor(image_np, cv2.COLOR_GRAY2RGB)
166
-
167
  # Convert RGB to BGR (OpenCV uses BGR by default)
168
  image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
169
 
@@ -172,8 +136,8 @@ def predict_image(image):
172
 
173
  # Transform the image for the model
174
  transform = transforms.Compose([
175
- transforms.ToTensor(),
176
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
177
  ])
178
  im = transform(image_resized).unsqueeze(0) # Add batch dimension (BCHW)
179
 
@@ -198,6 +162,10 @@ def predict_image(image):
198
  prediction_result = f"Predicted Class ID: {predicted_class_id}\nConfidence: {confidence_score:.4f}"
199
  description = "No description available."
200
 
 
 
 
 
201
  return prediction_result, description
202
 
203
  except Exception as e:
@@ -214,6 +182,83 @@ def get_description(class_name):
214
  }
215
  return descriptions.get(class_name.lower(), "No description available.")
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  # Gradio Interface
218
  def chatbot_ui():
219
  with gr.Blocks() as demo:
 
 
1
  from groq import Groq
2
  import gradio as gr
3
  from gtts import gTTS
 
9
  import spacy
10
  from transformers import pipeline
11
  import torch
12
+ from PIL import Image
 
13
  from torchvision import transforms
14
  import pathlib
15
+ import cv2 # Import OpenCV
16
+ import numpy as np
17
 
18
  # Pathlib adjustment for Windows compatibility
19
  temp = pathlib.PosixPath
 
30
  logger.addHandler(console_handler)
31
  logger.addHandler(file_handler)
32
 
33
+ #Initialize Groq Client
34
  client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
35
 
36
+ # # Initialize Groq Client
37
  #client = Groq(api_key="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")
38
 
39
  # Initialize spaCy NLP model for named entity recognition (NER)
40
+ nlp = spacy.load("en_core_web_sm")
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Initialize sentiment analysis model using Hugging Face
43
  sentiment_analyzer = pipeline("sentiment-analysis")
44
 
45
+ # Load pre-trained YOLOv5 model
 
 
46
  def load_yolov5_model():
 
47
  model = torch.hub.load(
48
+ r'C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\yolov5',
49
  'custom',
50
+ path=r"C:\Users\RESHMA R B\OneDrive\Documents\Desktop\project_without_malayalam\chatbot2\models\best.pt",
51
+ source="local"
52
  )
53
+ model.eval()
54
  return model
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  model = load_yolov5_model()
57
 
58
  # Function to preprocess user input for better NLP understanding
 
125
  if image is None:
126
  return "Error: No image uploaded.", "No description available."
127
 
128
+ # Convert PIL image to NumPy array (OpenCV format)
129
  image_np = np.array(image) # Convert PIL image to NumPy array
130
 
 
 
 
 
131
  # Convert RGB to BGR (OpenCV uses BGR by default)
132
  image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
133
 
 
136
 
137
  # Transform the image for the model
138
  transform = transforms.Compose([
139
+ transforms.ToTensor(), # Convert image to tensor
140
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), # Normalize
141
  ])
142
  im = transform(image_resized).unsqueeze(0) # Add batch dimension (BCHW)
143
 
 
162
  prediction_result = f"Predicted Class ID: {predicted_class_id}\nConfidence: {confidence_score:.4f}"
163
  description = "No description available."
164
 
165
+ # Display the image with OpenCV (optional)
166
+ cv2.imshow("Processed Image", image_resized)
167
+ cv2.waitKey(1) # Wait for 1 ms to display the image
168
+
169
  return prediction_result, description
170
 
171
  except Exception as e:
 
182
  }
183
  return descriptions.get(class_name.lower(), "No description available.")
184
 
185
+ # Custom LLM Bot Function
186
+ def customLLMBot(user_input, uploaded_image, chat_history):
187
+ try:
188
+ global messages
189
+ logger.info("Processing input...")
190
+
191
+ # Preprocess the user input
192
+ user_input = preprocess_input(user_input)
193
+
194
+ # Analyze sentiment (Optional)
195
+ sentiment = analyze_sentiment(user_input)
196
+ logger.info(f"Sentiment detected: {sentiment}")
197
+
198
+ # Extract medical entities (Optional)
199
+ medical_entities = extract_medical_entities(user_input)
200
+ logger.info(f"Extracted medical entities: {medical_entities}")
201
+
202
+ # Append user input to the chat history
203
+ chat_history.append(("user", user_input))
204
+
205
+ if uploaded_image is not None:
206
+ # Encode the image to base64
207
+ base64_image = encode_image(uploaded_image)
208
+
209
+ logger.debug(f"Image received, size: {len(base64_image)} bytes")
210
+
211
+ # Create a message for the image prompt
212
+ messages_image = [
213
+ {
214
+ "role": "user",
215
+ "content": [
216
+ {"type": "text", "text": "What's in this image?"},
217
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
218
+ ]
219
+ }
220
+ ]
221
+
222
+ logger.info("Sending image to Groq API for processing...")
223
+ response = client.chat.completions.create(
224
+ model="llama-3.2-11b-vision-preview",
225
+ messages=messages_image,
226
+ )
227
+ logger.info("Image processed successfully.")
228
+ else:
229
+ # Process text input
230
+ logger.info("Processing text input...")
231
+ messages.append({
232
+ "role": "user",
233
+ "content": user_input
234
+ })
235
+ response = client.chat.completions.create(
236
+ model="llama-3.2-11b-vision-preview",
237
+ messages=messages,
238
+ )
239
+ logger.info("Text processed successfully.")
240
+
241
+ # Extract the reply
242
+ LLM_reply = response.choices[0].message.content
243
+ logger.debug(f"LLM reply: {LLM_reply}")
244
+
245
+ # Append the bot's response to the chat history
246
+ chat_history.append(("bot", LLM_reply))
247
+ messages.append({"role": "assistant", "content": LLM_reply})
248
+
249
+ # Generate audio for response
250
+ audio_file = f"response_{uuid.uuid4().hex}.mp3"
251
+ tts = gTTS(LLM_reply, lang='en')
252
+ tts.save(audio_file)
253
+ logger.info(f"Audio response saved as {audio_file}")
254
+
255
+ # Return chat history and audio file
256
+ return chat_history, audio_file
257
+
258
+ except Exception as e:
259
+ logger.error(f"Error in customLLMBot function: {e}")
260
+ return [("user", user_input or "Image uploaded"), ("bot", f"An error occurred: {e}")], None
261
+
262
  # Gradio Interface
263
  def chatbot_ui():
264
  with gr.Blocks() as demo:
requirements.txt CHANGED
@@ -1,30 +1,29 @@
1
  # Core Libraries
2
- numpy==1.26.4
3
- pandas==2.2.2
4
- scipy==1.13.0
5
 
6
  # Machine Learning & Deep Learning
7
- torch==2.2.1
8
- torchvision==0.17.1
9
- transformers==4.39.3
10
- scikit-learn==1.4.1.post1
11
- ultralytics==8.1.27
12
 
13
  # Image Processing
14
- pillow==10.2.0
15
- opencv-python==4.9.0.80
16
 
17
  # NLP
18
- spacy==3.7.4
19
- https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.0/en_core_web_sm-3.7.0-py3-none-any.whl
20
 
21
  # Visualization
22
- matplotlib==3.8.3
23
 
24
  # Gradio & Audio
25
- gradio==4.24.0
26
- gtts==2.5.1
27
 
28
  # API Integration
29
- groq==0.15.0
30
- requests==2.31.0
 
1
  # Core Libraries
2
+ numpy
3
+ pandas
4
+ scipy
5
 
6
  # Machine Learning & Deep Learning
7
+ torch
8
+ torchvision
9
+ transformers
10
+ scikit-learn
11
+ ultralytics
12
 
13
  # Image Processing
14
+ pillow
15
+ opencv-python
16
 
17
  # NLP
18
+ spacy
 
19
 
20
  # Visualization
21
+ matplotlib
22
 
23
  # Gradio & Audio
24
+ gradio
25
+ gtts
26
 
27
  # API Integration
28
+ groq
29
+ requests