farhananis005 commited on
Commit
0b37ed1
1 Parent(s): 7a0607e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -0
app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+
4
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
5
+ os.environ["OPENAI_API_KEY"]
6
+ os.environ["GROQ_API_KEY"]
7
+
8
+ global agent
9
+
10
+
11
+ def create_agent():
12
+
13
+ from langchain_groq import ChatGroq
14
+ from langchain.chains.conversation.memory import ConversationSummaryBufferMemory
15
+ from langchain.chains import ConversationChain
16
+
17
+ global agent
18
+
19
+ llm = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768")
20
+ memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=1000)
21
+ agent = ConversationChain(llm=llm, memory=memory, verbose=True)
22
+
23
+ return "Successful!"
24
+
25
+
26
+ def formatted_response(docs, question, response, state):
27
+
28
+ formatted_output = response + "\n\nSources"
29
+
30
+ for i, doc in enumerate(docs):
31
+ source_info = doc.metadata.get("source", "Unknown source")
32
+ page_info = doc.metadata.get("page", None)
33
+
34
+ doc_name = source_info.split("/")[-1].strip()
35
+
36
+ if page_info is not None:
37
+ formatted_output += f"\n{doc_name}\tpage no {page_info}"
38
+ else:
39
+ formatted_output += f"\n{doc_name}"
40
+
41
+ state.append((question, formatted_output))
42
+ return state, state
43
+
44
+
45
+ def search_docs(prompt, question, state,k):
46
+
47
+ from langchain_openai import OpenAIEmbeddings
48
+ from langchain.vectorstores import FAISS
49
+ from langchain.callbacks import get_openai_callback
50
+
51
+ global agent
52
+ agent = agent
53
+
54
+ state = state or []
55
+
56
+ embeddings = OpenAIEmbeddings()
57
+ docs_db = FAISS.load_local(
58
+ "/content/drive/MyDrive/Art Story Merged DB",
59
+ embeddings,
60
+ allow_dangerous_deserialization=True
61
+ )
62
+ docs = docs_db.similarity_search(question,int(k))
63
+
64
+ prompt += "\n\n"
65
+ prompt += question
66
+ prompt += "\n\n"
67
+ prompt += str(docs)
68
+
69
+ with get_openai_callback() as cb:
70
+ response = agent.predict(input=prompt)
71
+ print(cb)
72
+
73
+ return formatted_response(docs, question, response, state)
74
+
75
+ import gradio as gr
76
+
77
+ css = """
78
+ .col{
79
+ max-width: 75%;
80
+ margin: 0 auto;
81
+ display: flex;
82
+ flex-direction: column;
83
+ justify-content: center;
84
+ align-items: center;
85
+ }
86
+ """
87
+
88
+ with gr.Blocks(css=css) as demo:
89
+ gr.Markdown("## <center>Your AI Medical Assistant</center>")
90
+
91
+ with gr.Tab("Your AI Assistant"):
92
+ with gr.Column(elem_classes="col"):
93
+
94
+ with gr.Tab("Query Documents"):
95
+ with gr.Column():
96
+ create_agent_button = gr.Button("Create Agent")
97
+ create_agent_output = gr.Textbox(label="Output")
98
+
99
+ docs_prompt_input = gr.Textbox(label="Custom Prompt")
100
+ k=gr.Textbox(label="Number of Chunks")
101
+
102
+ docs_chatbot = gr.Chatbot(label="Chats")
103
+ docs_state = gr.State()
104
+
105
+ docs_search_input = gr.Textbox(label="Question")
106
+ docs_search_button = gr.Button("Search")
107
+
108
+ gr.ClearButton(
109
+ [docs_prompt_input, docs_search_input, create_agent_output]
110
+ )
111
+
112
+ #########################################################################################################
113
+
114
+ create_agent_button.click(create_agent, inputs=None, outputs=create_agent_output)
115
+
116
+ docs_search_button.click(
117
+ search_docs,
118
+ inputs=[docs_prompt_input, docs_search_input, docs_state,k],
119
+ outputs=[docs_chatbot, docs_state],
120
+ )
121
+
122
+ #########################################################################################################
123
+
124
+ demo.queue()
125
+ demo.launch(debug=True)