Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import networkx as nx
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from groq import Groq
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
from langchain.chains import LLMChain
|
| 8 |
+
from langchain.llms import OpenAI
|
| 9 |
+
|
| 10 |
+
# Set up Groq API
|
| 11 |
+
api_key = st.secrets["GROQ_API_KEY"]
|
| 12 |
+
os.environ['GROQ_API_KEY'] = api_key
|
| 13 |
+
client = Groq()
|
| 14 |
+
|
| 15 |
+
def generate_response(prompt, context, max_tokens=1000):
|
| 16 |
+
messages = [
|
| 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(
|
| 31 |
+
input_variables=["text"],
|
| 32 |
+
template="Extract the main entities and their relationships from the following text:\n{text}\n\nEntities and relationships:"
|
| 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 = generate_response(entity_extraction_prompt.format(text=text), context="Entity extraction for knowledge graph")
|
| 42 |
+
|
| 43 |
+
# Parse the result and create a graph
|
| 44 |
+
G = nx.Graph()
|
| 45 |
+
|
| 46 |
+
# Simple parsing logic (you may need to adjust this based on the LLM's output format)
|
| 47 |
+
lines = result.split('\n')
|
| 48 |
+
for line in lines:
|
| 49 |
+
if '-' in line:
|
| 50 |
+
entity1, rest = line.split('-', 1)
|
| 51 |
+
relation, entity2 = rest.split(':', 1)
|
| 52 |
+
entity1 = entity1.strip()
|
| 53 |
+
entity2 = entity2.strip()
|
| 54 |
+
relation = relation.strip()
|
| 55 |
+
|
| 56 |
+
G.add_node(entity1)
|
| 57 |
+
G.add_node(entity2)
|
| 58 |
+
G.add_edge(entity1, entity2, relationship=relation)
|
| 59 |
+
|
| 60 |
+
return G
|
| 61 |
+
|
| 62 |
+
def visualize_graph(G):
|
| 63 |
+
pos = nx.spring_layout(G)
|
| 64 |
+
plt.figure(figsize=(12, 8))
|
| 65 |
+
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold')
|
| 66 |
+
edge_labels = nx.get_edge_attributes(G, 'relationship')
|
| 67 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
|
| 68 |
+
plt.title("Knowledge Graph")
|
| 69 |
+
plt.axis('off')
|
| 70 |
+
plt.tight_layout()
|
| 71 |
+
return plt
|
| 72 |
+
|
| 73 |
+
# Streamlit app
|
| 74 |
+
def main():
|
| 75 |
+
st.title("Knowledge Graph Generator")
|
| 76 |
+
|
| 77 |
+
text = st.text_area("Enter the text for analysis:", height=200)
|
| 78 |
+
|
| 79 |
+
if st.button("Generate Knowledge Graph"):
|
| 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 |
+
# Footer
|
| 92 |
+
st.markdown("""
|
| 93 |
+
<style>
|
| 94 |
+
footer {
|
| 95 |
+
margin-top: 20px;
|
| 96 |
+
text-align: center;
|
| 97 |
+
color: #bb86fc;
|
| 98 |
+
}
|
| 99 |
+
footer a {
|
| 100 |
+
color: #bb86fc !important;
|
| 101 |
+
text-decoration: none;
|
| 102 |
+
}
|
| 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 |
+
st.markdown(footer_text, unsafe_allow_html=True)
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|