File size: 2,705 Bytes
9dcb51d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# coding: utf-8

# In[ ]:





# In[16]:


import os
import gradio as gr
from swarmauri.standard.llms.concrete.GroqModel import GroqModel
from swarmauri.standard.conversations.concrete.MaxSystemContextConversation import MaxSystemContextConversation
from swarmauri.standard.conversations.concrete.Conversation import Conversation
from swarmauri.standard.vector_stores.concrete.TfidfVectorStore import TfidfVectorStore
from swarmauri.standard.messages.concrete.SystemMessage import SystemMessage
from swarmauri.standard.messages.concrete.HumanMessage import HumanMessage
from swarmauri.standard.documents.concrete.Document import Document
from swarmauri.standard.agents.concrete.RagAgent import RagAgent
from swarmauri.standard.agents.concrete.SimpleConversationAgent import SimpleConversationAgent


os.environ['GROQ_API_KEY'] = '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'
llm = GroqModel(api_key=os.environ['GROQ_API_KEY'])


# In[17]:


def extract_text_from_pdf(pdf_file_path):
    import fitz
    document = fitz.open(pdf_file_path)
    text = ""
    for page_num in range(document.page_count):
        page = document[page_num]
        text += page.get_text()
    document.close()
    return text


# In[25]:


def process_pdf(input_text, history, pdf_file):
    if pdf_file:
        temp_path = pdf_file.name
        text = extract_text_from_pdf(temp_path)

        system_context = SystemMessage(content='You are a RAG assistant that gives information about the pdf uploaded.')
        conversation = MaxSystemContextConversation(system_context=system_context)
        vector_store = TfidfVectorStore()
        documents = [Document(content=text)]
        vector_store.add_documents(documents)

        agent = RagAgent(
            llm=llm,
            conversation=conversation,
            system_context=system_context,
            vector_store=vector_store
        )
    else:
#         system_context = SystemMessage(content='You are a good conversation partner.')
        conversation = Conversation()
        initial_user_message = HumanMessage(content="Hello")
        conversation.add_message(initial_user_message)
        
        agent = SimpleConversationAgent(
            llm=llm,
            conversation=conversation
        )
    
    response = agent.exec(input_text)
    return str(response)


# In[26]:


def main():
    iface = gr.ChatInterface(
        fn=process_pdf,
        additional_inputs=gr.File(label="Upload PDF"),
        title="PDF Document Analyzer",
        description="Upload a PDF file to extract text and analyze it."
    )
    iface.launch(share=True)

if __name__ == "__main__":
    main()


# In[ ]:





# In[ ]: