Spaces:
Runtime error
Runtime error
Commit ·
64b9ebb
1
Parent(s): 1b6494c
ADD app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain.llms import OpenAI
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
#Pdf Loader
|
| 10 |
+
from langchain.document_loaders import PyPDFLoader
|
| 11 |
+
from langchain.vectorstores import Chroma
|
| 12 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 13 |
+
|
| 14 |
+
from langchain.agents.agent_toolkits import (
|
| 15 |
+
create_vectorstore_agent,
|
| 16 |
+
VectorStoreToolkit,
|
| 17 |
+
VectorStoreInfo
|
| 18 |
+
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
| 22 |
+
llm = OpenAI(temperature=0.4)
|
| 23 |
+
|
| 24 |
+
embeddings = OpenAIEmbeddings()
|
| 25 |
+
loader = PyPDFLoader('/content/2022_02_11_Montelogo Ida.pdf') # Baadme this will be updated from the Input @samunder
|
| 26 |
+
pages = loader.load_and_split()
|
| 27 |
+
store = Chroma.from_documents(pages,embeddings,collection_name='Montelogo_Ida')
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
vectorstore_info = VectorStoreInfo(
|
| 31 |
+
name = "Montelogo_Ida",
|
| 32 |
+
description = "Gpt on Montelogo Ida Pdf",
|
| 33 |
+
vectorstore = store
|
| 34 |
+
|
| 35 |
+
)
|
| 36 |
+
toolkit = VectorStoreToolkit(vectorstore_info=vectorstore_info)
|
| 37 |
+
|
| 38 |
+
agent_executor = create_vectorstore_agent(
|
| 39 |
+
llm=llm,
|
| 40 |
+
toolkit=toolkit,
|
| 41 |
+
verbose=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
chat_history = []
|
| 45 |
+
|
| 46 |
+
def generate_response(prompt, chat_history):
|
| 47 |
+
if prompt:
|
| 48 |
+
response = agent_executor.run(prompt)
|
| 49 |
+
chat_history.append((prompt, response))
|
| 50 |
+
|
| 51 |
+
return "",chat_history
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("Upload a document and Ask Questions")
|
| 56 |
+
with gr.Tab("Ask AI for cases based on Montelogo_Ida"):
|
| 57 |
+
gr.Markdown("This answers based on this ")
|
| 58 |
+
chatbot = gr.Chatbot()
|
| 59 |
+
msg = gr.Textbox("Chat with me ❤️")
|
| 60 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 61 |
+
|
| 62 |
+
msg.submit(generate_response, [msg, chatbot], [msg, chatbot])
|
| 63 |
+
with gr.Tab("Upload ANY Pdf and Ask AI"):
|
| 64 |
+
gr.Markdown("Work in Progress")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
# file_input = gr.File()
|
| 68 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 69 |
+
btn = gr.UploadButton("📁", file_types=["pdf", "image", "video"])
|
| 70 |
+
btn.upload()
|
| 71 |
+
# pdf_path = '/content/2022_02_11_Montelogo Ida.pdf'
|
| 72 |
+
# pdf_viewer = gr.FileViewer(pdf_path)
|
| 73 |
+
|
| 74 |
+
# image_output = gr.Image()
|
| 75 |
+
# image_button = gr.Button("Flip")
|
| 76 |
+
|
| 77 |
+
# with gr.Accordion("Open for More!"):
|
| 78 |
+
# gr.Markdown("Look at me...")
|
| 79 |
+
|
| 80 |
+
# iface = gr.Interface(
|
| 81 |
+
# fn=generate_response,
|
| 82 |
+
# inputs=gr.inputs.Textbox(label="User Message", placeholder="Enter your message..."),
|
| 83 |
+
# outputs=gr.outputs.Textbox(label="Generated AI Response"),
|
| 84 |
+
# title="Legal Case Chat",
|
| 85 |
+
# description="Enter your legal case context and get AI-generated responses.",
|
| 86 |
+
# theme="compact",
|
| 87 |
+
# layout="vertical",
|
| 88 |
+
# width="auto",
|
| 89 |
+
# height=400,
|
| 90 |
+
# show_tips=False
|
| 91 |
+
# )
|
| 92 |
+
|
| 93 |
+
demo.launch(share=True)
|