Dharun72 commited on
Commit
b94c328
·
verified ·
1 Parent(s): 7196f45
Files changed (5) hide show
  1. .env +1 -0
  2. app.py +105 -0
  3. breed2.pdf +0 -0
  4. requirements.txt +8 -0
  5. version4c.pt +3 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GROQ_API_KEY='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'
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FINAL COMBINE GRADIO INTERFACE
2
+
3
+ import PIL.Image as Image
4
+ import gradio as gr
5
+ from ultralytics import YOLO
6
+ import os
7
+ import time
8
+ from langchain_groq import ChatGroq
9
+ from langchain_community.embeddings import HuggingFaceEmbeddings
10
+ from langchain.text_splitter import CharacterTextSplitter
11
+ from langchain.chains.combine_documents import create_stuff_documents_chain
12
+ from langchain_core.prompts import ChatPromptTemplate
13
+ from langchain.chains import create_retrieval_chain
14
+ from langchain_community.vectorstores import FAISS
15
+ from langchain_community.document_loaders import PyPDFLoader
16
+ from dotenv import load_dotenv
17
+
18
+ load_dotenv()
19
+ groq_api_key = os.getenv('GROQ_API_KEY')
20
+
21
+ # Initialize object detection model
22
+ model = YOLO("version4c.pt")
23
+
24
+ def predict_image(img, conf_threshold, iou_threshold):
25
+ # Perform object detection
26
+ results = model.predict(source=img, conf=conf_threshold, iou=iou_threshold, show_labels=True, show_conf=True, imgsz=640)
27
+ # Plot the result
28
+ for r in results:
29
+ im_array = r.plot()
30
+ im = Image.fromarray(im_array[..., ::-1])
31
+ return im
32
+
33
+ # Initialize chatbot components
34
+ llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
35
+ prompt = ChatPromptTemplate.from_template(
36
+ """ Answer the questions based on the provided context only. Please provide the most accurate response based on the question <context> {context} <context> Questions:{input} """
37
+ )
38
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
39
+ loader = PyPDFLoader("breed2.pdf")
40
+ docs = loader.load()
41
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
42
+ final_documents = text_splitter.split_documents(docs)
43
+ # Extract text content from the Document instances
44
+ doc_texts = [doc.page_content for doc in final_documents]
45
+ embeddings_result = embeddings.embed_documents(doc_texts)
46
+ if embeddings_result:
47
+ vectors = FAISS.from_documents(final_documents, embeddings)
48
+ else:
49
+ raise ValueError("Failed to generate embeddings. Please check your input documents or try a different embedding model.")
50
+
51
+ document_chain = create_stuff_documents_chain(llm, prompt)
52
+ retriever = vectors.as_retriever()
53
+ retrieval_chain = create_retrieval_chain(retriever, document_chain)
54
+
55
+ def print_like_dislike(x: gr.LikeData):
56
+ print(x.index, x.value, x.liked)
57
+
58
+ def add_message(history, message):
59
+ for x in message["files"]:
60
+ history.append(((x,), None))
61
+ if message["text"] is not None:
62
+ history.append((message["text"], None))
63
+ return history, gr.MultimodalTextbox(value=None, interactive=False)
64
+
65
+ def bot(history):
66
+ message = history[-1][0]
67
+ start_time = time.time()
68
+ response = retrieval_chain.invoke({'input': message})['answer']
69
+ response_time = time.time() - start_time
70
+ if response_time > 6:
71
+ return [(f"Sorry, I couldn't generate a response within 6 seconds. Please try again.", None)]
72
+ history[-1][1] = ""
73
+ for character in response:
74
+ history[-1][1] += character
75
+ time.sleep(0.05)
76
+ yield history
77
+
78
+ with gr.Blocks() as demo:
79
+ with gr.Row():
80
+ with gr.Column(scale=2):
81
+ model_input = gr.Image(type="pil", label="Upload Image")
82
+ conf_threshold = gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold")
83
+ iou_threshold = gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
84
+ model_output = gr.Image(type="pil", label="Result")
85
+
86
+ model_btn = gr.Button("Detect Objects")
87
+ model_btn.click(predict_image, inputs=[model_input, conf_threshold, iou_threshold], outputs=model_output)
88
+
89
+ with gr.Column(scale=1):
90
+ chatbot = gr.Chatbot(
91
+ [],
92
+ elem_id="chatbot",
93
+ bubble_full_width=False
94
+ )
95
+
96
+ chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
97
+ chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
98
+ bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
99
+ bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
100
+ chatbot.like(print_like_dislike, None, None)
101
+
102
+ demo.queue()
103
+
104
+ if _name_ == "_main_":
105
+ demo.launch()
breed2.pdf ADDED
Binary file (955 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ ultralytics
3
+ opencv-python
4
+ pypdf
5
+ langchain_groq
6
+ langchain_community
7
+ langchain==0.1.17
8
+ HuggingFaceEmbeddings
version4c.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b65db46eaa7e2228f3ffca2c97f720582c1906156964c060c7dbf38349b4bde4
3
+ size 87692713