Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
import textrazor
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import pandas as pd
|
6 |
+
import re
|
7 |
+
|
8 |
+
# Set TextRazor API key using Streamlit Secrets
|
9 |
+
textrazor.api_key = st.secrets["textrazer_api"]
|
10 |
+
|
11 |
+
# Streamlit app title and input for URL
|
12 |
+
st.title("TextRazor Entities Analysis")
|
13 |
+
url_input = st.text_input("Enter URL to Analyze")
|
14 |
+
|
15 |
+
# Check if URL is provided
|
16 |
+
if url_input:
|
17 |
+
# Create TextRazor client and analyze URL
|
18 |
+
client = textrazor.TextRazor(extractors=["entities", "topics"])
|
19 |
+
response = client.analyze_url(url_input)
|
20 |
+
|
21 |
+
# Create dataframe and lists
|
22 |
+
df = pd.DataFrame(columns=['Entity ID', 'Entity Relevance', 'Entity Confidence', 'Entity Freebase'])
|
23 |
+
new_entityid = []
|
24 |
+
new_relevance = []
|
25 |
+
new_confidence = []
|
26 |
+
new_freebase = []
|
27 |
+
|
28 |
+
# Loop entities and append to list
|
29 |
+
for entity in response.entities():
|
30 |
+
entity_id = re.sub(r'\d+', '', entity.id)
|
31 |
+
new_entityid.append(entity_id)
|
32 |
+
new_relevance.append(entity.relevance_score)
|
33 |
+
new_confidence.append(entity.confidence_score)
|
34 |
+
new_freebase.append(entity.freebase_types)
|
35 |
+
|
36 |
+
# Assign columns values from lists to dataframe
|
37 |
+
df['Entity ID'] = new_entityid
|
38 |
+
df['Entity Relevance'] = new_relevance
|
39 |
+
df['Entity Confidence'] = new_confidence
|
40 |
+
df['Entity Freebase'] = new_freebase
|
41 |
+
|
42 |
+
# Save data to CSV
|
43 |
+
df.to_csv('textrazor_v1_entities.csv')
|
44 |
+
|
45 |
+
# Display dataframe
|
46 |
+
st.write("Top 25 Entities:")
|
47 |
+
st.write(df.head(25))
|
48 |
+
|
49 |
+
# Plot the top 10 entities
|
50 |
+
st.bar_chart(df['Entity ID'].value_counts().nlargest(10))
|
51 |
+
|
52 |
+
# Add labels and title to the chart
|
53 |
+
plt.xlabel('Entity')
|
54 |
+
plt.ylabel('Frequency')
|
55 |
+
plt.title('Top 10 Entities by frequency')
|
56 |
+
|
57 |
+
# Show the plot in Streamlit
|
58 |
+
st.pyplot(plt)
|
59 |
+
else:
|
60 |
+
st.warning("Please enter a URL to analyze.")
|