File size: 15,313 Bytes
4eba3d2 e107ee4 4eba3d2 e107ee4 4eba3d2 e107ee4 4eba3d2 e107ee4 4eba3d2 e107ee4 4eba3d2 e107ee4 |
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# import streamlit as st
# import pandas as pd
# import networkx as nx
# from bokeh.models import HoverTool
# from bokeh.plotting import figure, from_networkx
# import requests
# import json
# import google.generativeai as genai
# PERPLEXITY_API_KEY = "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"
# PERPLEXITY_API_URL = "https://api.perplexity.ai/chat/completions"
# def extract_edges(keywords):
# keywords = [kw.strip() for kw in keywords.split(",")]
# edges = [
# (keywords[i], keywords[j])
# for i in range(len(keywords))
# for j in range(i + 1, len(keywords))
# ]
# return edges
# def create_knowledge_graph(data):
# G = nx.Graph()
# for _, row in data.iterrows():
# words = []
# for col in data.columns:
# if pd.notnull(row[col]):
# # Convert to string and handle numeric values
# cell_value = str(row[col]).strip()
# if cell_value:
# words.extend(cell_value.split())
# if words:
# edges = extract_edges(",".join(words))
# G.add_edges_from(edges)
# for word in words:
# word = word.strip()
# if word not in G:
# G.add_node(word, title=word, value=len(word))
# return G
# def render_graph_bokeh(G):
# plot = figure(
# title="Interactive Knowledge Graph",
# x_range=(-1.5, 1.5),
# y_range=(-1.5, 1.5),
# tools="pan,wheel_zoom,box_zoom,reset,tap",
# active_scroll="wheel_zoom",
# )
# plot.add_tools(HoverTool(tooltips="@index"))
# graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
# graph_renderer.node_renderer.glyph.size = 10
# graph_renderer.node_renderer.glyph.fill_color = "blue"
# graph_renderer.node_renderer.glyph.line_color = "black"
# graph_renderer.edge_renderer.glyph.line_width = 1
# graph_renderer.edge_renderer.glyph.line_color = "gray"
# plot.renderers.append(graph_renderer)
# return plot
# import re
# def search_papers(topic: str, num_papers: int) -> list:
# headers = {
# "Authorization": f"Bearer {PERPLEXITY_API_KEY}",
# "Content-Type": "application/json",
# }
# prompt = f"""Find {num_papers} recent research papers about {topic}.
# Return ONLY a valid JSON array with the following structure for each paper:
# [
# {{
# "Title": "paper title",
# "Abstract": "abstract text",
# "Keywords": "key terms"
# }}
# ]"""
# payload = {
# "model": "llama-3.1-sonar-small-128k-chat",
# "messages": [
# {
# "role": "system",
# "content": "You are a research paper analyzer that returns valid JSON arrays.",
# },
# {"role": "user", "content": prompt},
# ],
# "temperature": 0.1,
# }
# try:
# response = requests.post(PERPLEXITY_API_URL, headers=headers, json=payload)
# response.raise_for_status()
# content = response.json()["choices"][0]["message"]["content"]
# # Clean response to ensure valid JSON
# content = content.strip()
# if not content.startswith("["):
# content = content[content.find("[") :]
# if not content.endswith("]"):
# content = content[: content.rfind("]") + 1]
# # Remove any trailing commas before closing brackets
# content = re.sub(r",\s*]", "]", content)
# content = re.sub(r",\s*}", "}", content)
# papers = json.loads(content)
# if not isinstance(papers, list):
# raise ValueError("Response is not a JSON array")
# return papers
# except requests.exceptions.RequestException as e:
# st.error(f"API Request Error: {str(e)}")
# return []
# except json.JSONDecodeError as e:
# st.error(f"Invalid JSON response: {str(e)}")
# st.error(f"Response content: {response.text}")
# return []
# except ValueError as e:
# st.error(f"Error: {str(e)}")
# return []
# import os
# GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
# GEMINI_API_URL = "https://api.openai.com/v1/engines/davinci-codex/completions"
# def call_gemini_api(prompt: str) -> str:
# headers = {
# "Authorization": f"Bearer {GEMINI_API_KEY}",
# "Content-Type": "application/json",
# }
# payload = {
# "prompt": prompt,
# "max_tokens": 150,
# "temperature": 0.7,
# }
# try:
# model = genai.GenerativeModel("gemini-pro")
# response = model.generate_content(prompt)
# return response.text
# except Exception as e:
# st.error(f"Gemini API Error: {str(e)}")
# return ""
# def generate_gaps_paragraph(gaps):
# prompt = f"Generate a brief paragraph about the gaps in the research based on the following gaps: {', '.join(gaps)}"
# return call_gemini_api(prompt)
# def generate_insights(G, topic):
# papers = search_papers(topic, 5)
# if papers:
# st.write("### Research Insights from Perplexity API")
# for paper in papers:
# st.write(f"**Title:** {paper['Title']}")
# st.write(f"**Abstract:** {paper['Abstract']}")
# st.write(f"**Keywords:** {paper['Keywords']}")
# st.write("---")
# nodes = list(G.nodes(data=True))
# insights = {}
# insights["Strong Points"] = [
# n for n, d in nodes if G.degree(n) > len(G.nodes) * 0.1
# ]
# insights["Weak Points"] = [n for n, d in nodes if G.degree(n) < len(G.nodes) * 0.05]
# insights["Gaps"] = [n for n, d in nodes if len(list(nx.neighbors(G, n))) == 0]
# st.write("### Graph-Based Insights")
# st.write("**Strong Points:**", insights["Strong Points"])
# st.write("**Weak Points:**", insights["Weak Points"])
# st.write("**Gaps:**", insights["Gaps"])
# if insights["Gaps"]:
# with st.spinner("Generating insights about gaps..."):
# gaps_paragraph = generate_gaps_paragraph(insights["Gaps"])
# if gaps_paragraph:
# st.write("### Gaps in Research")
# st.write(gaps_paragraph)
# def main():
# st.title("Advanced Interactive Knowledge Graph")
# st.write(
# "Upload a CSV file to generate a fully interactive and insightful knowledge graph."
# )
# uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
# if uploaded_file is not None:
# try:
# data = pd.read_csv(uploaded_file)
# st.write("Preview of the uploaded data:")
# st.dataframe(data.head())
# G = create_knowledge_graph(data)
# st.write("Generated Knowledge Graph:")
# plot = render_graph_bokeh(G)
# st.bokeh_chart(plot, use_container_width=True)
# topic = st.text_input(
# "Enter a topic for additional insights:", "knowledge graphs"
# )
# if topic:
# generate_insights(G, topic)
# except Exception as e:
# st.error(f"An error occurred while processing the file: {e}")
# else:
# st.info("Please upload a CSV file to get started.")
# if __name__ == "__main__":
# main()
import streamlit as st
import pandas as pd
import networkx as nx
from bokeh.models import HoverTool
from bokeh.plotting import figure, from_networkx
import requests
import json
import google.generativeai as genai
from dotenv import load_dotenv
import os
load_dotenv()
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")
PERPLEXITY_API_URL = "https://api.perplexity.ai/chat/completions"
def extract_edges(keywords):
keywords = [kw.strip() for kw in keywords.split(",")]
edges = [
(keywords[i], keywords[j])
for i in range(len(keywords))
for j in range(i + 1, len(keywords))
]
return edges
def create_knowledge_graph(data):
G = nx.Graph()
for _, row in data.iterrows():
words = []
for col in data.columns:
if pd.notnull(row[col]):
# Convert to string and handle numeric values
cell_value = str(row[col]).strip()
if cell_value:
words.extend(cell_value.split())
if words:
edges = extract_edges(",".join(words))
G.add_edges_from(edges)
for word in words:
word = word.strip()
if word not in G:
G.add_node(word, title=word, value=len(word))
return G
def render_graph_bokeh(G):
plot = figure(
title="Interactive Knowledge Graph",
x_range=(-1.5, 1.5),
y_range=(-1.5, 1.5),
tools="pan,wheel_zoom,box_zoom,reset,tap",
active_scroll="wheel_zoom",
)
plot.add_tools(HoverTool(tooltips="@index"))
graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
graph_renderer.node_renderer.glyph.size = 10
graph_renderer.node_renderer.glyph.fill_color = "blue"
graph_renderer.node_renderer.glyph.line_color = "black"
graph_renderer.edge_renderer.glyph.line_width = 1
graph_renderer.edge_renderer.glyph.line_color = "gray"
plot.renderers.append(graph_renderer)
return plot
import re
def search_papers(topic: str, num_papers: int) -> list:
headers = {
"Authorization": f"Bearer {PERPLEXITY_API_KEY}",
"Content-Type": "application/json",
}
prompt = f"""Find {num_papers} recent research papers about {topic}.
Return ONLY a valid JSON array with the following structure for each paper:
[
{{
"Title": "paper title",
"Abstract": "abstract text",
"Keywords": "key terms"
}}
]"""
payload = {
"model": "llama-3.1-sonar-small-128k-chat",
"messages": [
{
"role": "system",
"content": "You are a research paper analyzer that returns valid JSON arrays.",
},
{"role": "user", "content": prompt},
],
"temperature": 0.1,
}
try:
response = requests.post(PERPLEXITY_API_URL, headers=headers, json=payload)
response.raise_for_status()
content = response.json()["choices"][0]["message"]["content"]
# Clean response to ensure valid JSON
content = content.strip()
if not content.startswith("["):
content = content[content.find("[") :]
if not content.endswith("]"):
content = content[: content.rfind("]") + 1]
# Remove any trailing commas before closing brackets
content = re.sub(r",\s*]", "]", content)
content = re.sub(r",\s*}", "}", content)
papers = json.loads(content)
if not isinstance(papers, list):
raise ValueError("Response is not a JSON array")
return papers
except requests.exceptions.RequestException as e:
st.error(f"API Request Error: {str(e)}")
return []
except json.JSONDecodeError as e:
st.error(f"Invalid JSON response: {str(e)}")
st.error(f"Response content: {response.text}")
return []
except ValueError as e:
st.error(f"Error: {str(e)}")
return []
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
GEMINI_API_URL = "https://api.openai.com/v1/engines/davinci-codex/completions"
def call_gemini_api(prompt: str) -> str:
headers = {
"Authorization": f"Bearer {GEMINI_API_KEY}",
"Content-Type": "application/json",
}
payload = {
"prompt": prompt,
"max_tokens": 150,
"temperature": 0.7,
}
try:
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(prompt)
return response.text
except Exception as e:
st.error(f"Gemini API Error: {str(e)}")
return ""
def generate_gaps_paragraph(gaps):
prompt = f"Generate a brief paragraph about the gaps in the research based on the following gaps provide evidence-based(how did you deduce this) recommendations for new research paper ideas based on these gaps, Justify your recommendations with evidence from the data it analyzes. Give atleast 10 new research paper ideas based on the gaps and 500 words gap analysis,Give a different table for new research ideas with evidence that why did you reccommend it, In the new Table Containing Research Paper Ideas there should be 4 columns - New Research Paper Idea/Title,Evidence , Methodology(How will we approach the ideo new research paper), Evidence Of How Methodology was obtained: {', '.join(gaps)}"
return call_gemini_api(prompt)
def generate_insights(G, topic):
papers = search_papers(topic, 5)
if papers:
st.write("### Research Insights from Perplexity API")
for paper in papers:
st.write(f"**Title:** {paper['Title']}")
st.write(f"**Abstract:** {paper['Abstract']}")
st.write(f"**Keywords:** {paper['Keywords']}")
st.write("---")
nodes = list(G.nodes(data=True))
insights = {}
insights["Strong Points"] = [
n for n, d in nodes if G.degree(n) > len(G.nodes) * 0.1
]
insights["Weak Points"] = [n for n, d in nodes if G.degree(n) < len(G.nodes) * 0.05]
insights["Gaps"] = [n for n, d in nodes if len(list(nx.neighbors(G, n))) == 0]
st.write("### Graph-Based Insights")
st.write("**Strong Points:**", insights["Strong Points"])
st.write("**Weak Points:**", insights["Weak Points"])
st.write("**Gaps:**", insights["Gaps"])
if insights["Gaps"]:
with st.spinner("Generating insights about gaps..."):
gaps_paragraph = generate_gaps_paragraph(insights["Gaps"])
if gaps_paragraph:
st.write("### Gaps in Research")
st.write(gaps_paragraph)
st.download_button(
label="Download Gaps Analysis as Text",
data=gaps_paragraph,
file_name="gaps_analysis.txt",
mime="text/plain",
)
def main():
st.title("Advanced Interactive Knowledge Graph")
st.write(
"Upload a CSV file to generate a fully interactive and insightful knowledge graph."
)
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is not None:
try:
data = pd.read_csv(uploaded_file)
st.write("Preview of the uploaded data:")
st.dataframe(data.head())
G = create_knowledge_graph(data)
st.write("Generated Knowledge Graph:")
plot = render_graph_bokeh(G)
st.bokeh_chart(plot, use_container_width=True)
topic = st.text_input(
"Enter a topic for additional insights:", "knowledge graphs"
)
if topic:
generate_insights(G, topic)
except Exception as e:
st.error(f"An error occurred while processing the file: {e}")
else:
st.info("Please upload a CSV file to get started.")
if __name__ == "__main__":
main()
|