File size: 3,996 Bytes
741b8ab
 
da07d96
a66e192
741b8ab
 
 
a66e192
 
 
741b8ab
 
 
 
 
a66e192
741b8ab
 
a66e192
 
 
741b8ab
a66e192
741b8ab
a66e192
 
741b8ab
a66e192
 
 
 
741b8ab
 
a66e192
 
 
741b8ab
a66e192
741b8ab
a66e192
 
 
 
 
 
741b8ab
 
a66e192
741b8ab
a66e192
 
741b8ab
a66e192
 
 
 
741b8ab
a66e192
 
 
 
741b8ab
 
a66e192
 
 
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
 
741b8ab
a66e192
741b8ab
a66e192
 
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
 
741b8ab
a66e192
741b8ab
a66e192
741b8ab
a66e192
 
 
 
 
 
741b8ab
a66e192
741b8ab
 
a66e192
741b8ab
a66e192
da07d96
a66e192
741b8ab
da07d96
741b8ab
a66e192
da07d96
 
 
 
 
 
741b8ab
da07d96
 
741b8ab
da07d96
 
741b8ab
da07d96
 
741b8ab
da07d96
a66e192
da07d96
a66e192
da07d96
a66e192
da07d96
 
 
 
 
 
a66e192
da07d96
741b8ab
da07d96
 
741b8ab
da07d96
 
a66e192
da07d96
 
a66e192
da07d96
 
a66e192
da07d96
a66e192
da07d96
a66e192
da07d96
a66e192
da07d96
741b8ab
 
a66e192
 
 
 
 
 
 
 
 
 
 
da07d96
a66e192
da07d96
 
 
a66e192
da07d96
a66e192
da07d96
741b8ab
da07d96
 
 
741b8ab
da07d96
741b8ab
da07d96
a66e192
 
da07d96
 
 
 
 
a66e192
da07d96
 
a66e192
da07d96
 
a66e192
da07d96
a66e192
da07d96
741b8ab
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import streamlit as st
from groq import Groq
import json
from src.embeddings import embedding_model
from src.vector_store import VectorStore


# =====================================================
# GROQ CLIENT
# =====================================================

client = Groq(
    api_key=st.secrets["GROQ_API_KEY"]
)

vector_db = VectorStore()


# =====================================================
# CHUNKING
# =====================================================

def chunk_text(text, chunk_size=1000):

    if not text:
        return []

    return [
        text[i:i + chunk_size]
        for i in range(0, len(text), chunk_size)
    ]


# =====================================================
# VECTOR DATABASE
# =====================================================

def create_rag_database(papers):

    documents = []

    for paper in papers:

        chunks = chunk_text(
            paper.get("text", "")
        )

        documents.extend(chunks)

    if not documents:
        return

    embeddings = embedding_model.encode(
        documents,
        show_progress_bar=False
    )

    vector_db.build(
        embeddings,
        documents
    )


# =====================================================
# CHAT WITH PAPERS
# =====================================================

def ask_rag(question):

    query_embedding = embedding_model.encode(question)

    context = vector_db.search(query_embedding)

    context_text = "\n\n".join(context)

    prompt = f"""
You are an expert scientific research assistant.

Answer ONLY from the provided research paper.

If the answer is not available,
reply:

"I could not find this information in the uploaded paper."

Context:

{context_text}

Question:

{question}
"""

    response = client.chat.completions.create(

        model="llama-3.1-8b-instant",

        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ],

        temperature=0.2
    )

    return response.choices[0].message.content

# =====================================================
# COMPLETE PAPER ANALYSIS (Single AI Call)
# =====================================================

def analyze_paper(text, abstract=""):

    if not text:
        return {
            "summary": "No text available.",
            "abstract_summary": "No abstract available.",
            "limitations": "Not available.",
            "research_gaps": "Not available."
        }

    # Use only a limited amount of text to stay within token limits
    paper_text = text[:4000]

    if abstract:
        abstract = abstract[:1500]

    prompt = f"""
You are an expert scientific research assistant.

Analyze the following research paper.

Return ONLY valid JSON.

The JSON must have EXACTLY these keys:

{{
  "summary": "...",
  "abstract_summary": "...",
  "limitations": "...",
  "research_gaps": "..."
}}

Instructions:

- abstract_summary:
Summarize ONLY the abstract in 4-5 sentences.

- summary:
A concise summary (150-200 words).

- limitations:
List the main 2-3 limitations as bullet points.

- research_gaps:
List 3 future research directions.

ABSTRACT:

{abstract}

PAPER:

{paper_text}
"""

    response = client.chat.completions.create(

        model="llama-3.1-8b-instant",

        messages=[
            {
                "role": "user",
                "content": prompt
            }
        ],

        temperature=0.2,

        response_format={
            "type": "json_object"
        }

    )

    try:

        result = json.loads(
            response.choices[0].message.content
        )

    except Exception:

        result = {


            "abstract_summary":
                "Abstract summary unavailable.",
            
            "summary":
                "Summary generation failed.",

            "limitations":
                "Limitations unavailable.",

            "research_gaps":
                "Research gaps unavailable."

        }

    return result