Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,18 @@
|
|
| 1 |
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
import networkx as nx
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
from
|
| 6 |
-
from langchain.prompts import PromptTemplate
|
| 7 |
from langchain.chains import LLMChain
|
| 8 |
-
from langchain.
|
|
|
|
| 9 |
|
| 10 |
-
# Set up
|
| 11 |
-
api_key =
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
{"role": "system", "content": "You are a helpful coding assistant, specialized in code completion, debugging, and analysis. Provide concise and accurate responses."},
|
| 18 |
-
{"role": "user", "content": f"Context: {context}\n\nTask: {prompt}"}
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
-
chat_completion = client.chat.completions.create(
|
| 22 |
-
messages=messages,
|
| 23 |
-
model="llama2-70b-4096",
|
| 24 |
-
max_tokens=max_tokens
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
return chat_completion.choices[0].message.content
|
| 28 |
|
| 29 |
# Define a prompt template for entity extraction
|
| 30 |
entity_extraction_prompt = PromptTemplate(
|
|
@@ -33,12 +21,11 @@ entity_extraction_prompt = PromptTemplate(
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# Create an LLMChain for entity extraction
|
| 36 |
-
llm = OpenAI(temperature=0)
|
| 37 |
entity_chain = LLMChain(llm=llm, prompt=entity_extraction_prompt)
|
| 38 |
|
| 39 |
def create_knowledge_graph(text):
|
| 40 |
# Extract entities and relationships
|
| 41 |
-
result =
|
| 42 |
|
| 43 |
# Parse the result and create a graph
|
| 44 |
G = nx.Graph()
|
|
@@ -70,31 +57,32 @@ def visualize_graph(G):
|
|
| 70 |
plt.tight_layout()
|
| 71 |
return plt
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
if text:
|
| 81 |
-
with st.spinner("Generating knowledge graph..."):
|
| 82 |
-
knowledge_graph = create_knowledge_graph(text)
|
| 83 |
-
st.success("Knowledge graph generated!")
|
| 84 |
-
|
| 85 |
-
st.subheader("Visualization")
|
| 86 |
-
fig = visualize_graph(knowledge_graph)
|
| 87 |
-
st.pyplot(fig)
|
| 88 |
-
else:
|
| 89 |
-
st.warning("Please enter some text for analysis.")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
footer {
|
| 95 |
margin-top: 20px;
|
| 96 |
text-align: center;
|
| 97 |
color: #bb86fc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
footer a {
|
| 100 |
color: #bb86fc !important;
|
|
@@ -103,17 +91,12 @@ def main():
|
|
| 103 |
footer a:hover {
|
| 104 |
text-decoration: underline;
|
| 105 |
}
|
| 106 |
-
</style>
|
| 107 |
-
""", unsafe_allow_html=True)
|
| 108 |
-
|
| 109 |
-
footer_text = """
|
| 110 |
-
<footer>
|
| 111 |
-
<p>If you enjoyed the functionality of the app, please leave a like!<br>
|
| 112 |
-
Check out more on <a href="https://www.linkedin.com/in/girish-wangikar/" target="_blank">LinkedIn</a> |
|
| 113 |
-
<a href="https://girishwangikar.github.io/Girish_Wangikar_Portfolio.github.io/" target="_blank">Portfolio</a></p>
|
| 114 |
-
</footer>
|
| 115 |
"""
|
| 116 |
-
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import networkx as nx
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from langchain.llms import ChatGroq
|
|
|
|
| 5 |
from langchain.chains import LLMChain
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
import gradio as gr
|
| 8 |
|
| 9 |
+
# Set up the ChatGroq API
|
| 10 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 11 |
+
if not api_key:
|
| 12 |
+
raise ValueError("Please set the GROQ_API_KEY environment variable")
|
| 13 |
|
| 14 |
+
# Initialize the ChatGroq LLM
|
| 15 |
+
llm = ChatGroq(temperature=0, model_name='llama-3.1-8b-instant', groq_api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Define a prompt template for entity extraction
|
| 18 |
entity_extraction_prompt = PromptTemplate(
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
# Create an LLMChain for entity extraction
|
|
|
|
| 24 |
entity_chain = LLMChain(llm=llm, prompt=entity_extraction_prompt)
|
| 25 |
|
| 26 |
def create_knowledge_graph(text):
|
| 27 |
# Extract entities and relationships
|
| 28 |
+
result = entity_chain.run(text)
|
| 29 |
|
| 30 |
# Parse the result and create a graph
|
| 31 |
G = nx.Graph()
|
|
|
|
| 57 |
plt.tight_layout()
|
| 58 |
return plt
|
| 59 |
|
| 60 |
+
def generate_knowledge_graph(text):
|
| 61 |
+
if text:
|
| 62 |
+
knowledge_graph = create_knowledge_graph(text)
|
| 63 |
+
fig = visualize_graph(knowledge_graph)
|
| 64 |
+
return fig
|
| 65 |
+
else:
|
| 66 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# Gradio interface
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=generate_knowledge_graph,
|
| 71 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter your text here..."),
|
| 72 |
+
outputs=gr.Plot(),
|
| 73 |
+
title="Knowledge Graph Generator",
|
| 74 |
+
description="Enter text to generate a knowledge graph.",
|
| 75 |
+
theme="default",
|
| 76 |
+
css="""
|
| 77 |
footer {
|
| 78 |
margin-top: 20px;
|
| 79 |
text-align: center;
|
| 80 |
color: #bb86fc;
|
| 81 |
+
position: fixed;
|
| 82 |
+
bottom: 0;
|
| 83 |
+
width: 100%;
|
| 84 |
+
background-color: white;
|
| 85 |
+
padding: 10px 0;
|
| 86 |
}
|
| 87 |
footer a {
|
| 88 |
color: #bb86fc !important;
|
|
|
|
| 91 |
footer a:hover {
|
| 92 |
text-decoration: underline;
|
| 93 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
"""
|
| 95 |
+
)
|
| 96 |
|
| 97 |
+
# Custom footer
|
| 98 |
+
footer_html = """
|
| 99 |
+
<footer>
|
| 100 |
+
<p>If you enjoyed the functionality of the app, please leave a like!<br>
|
| 101 |
+
Check out more on <a href="https://www.linkedin.com/in/girish-wangikar/" target="_blank">LinkedIn</a> |
|
| 102 |
+
<a href="https://girishwangikar.github.io/Girish_Wangikar_Portfolio.github.io/" target="_blank">Portfolio</a></p>
|