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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
from together import Together
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import re
|
| 9 |
+
|
| 10 |
+
# -------------------#
|
| 11 |
+
# Secure API key load
|
| 12 |
+
# -------------------#
|
| 13 |
+
load_dotenv()
|
| 14 |
+
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY", "987adcf573b9658c775b671270aef959b3d38793771932f372f9f2a9ed5b78bf")
|
| 15 |
+
client = Together(api_key=TOGETHER_API_KEY)
|
| 16 |
+
|
| 17 |
+
# -------------------#
|
| 18 |
+
# Streamlit UI setup
|
| 19 |
+
# -------------------#
|
| 20 |
+
st.set_page_config(page_title="FutureScope: Research Direction Explorer", layout="wide")
|
| 21 |
+
|
| 22 |
+
st.markdown("""
|
| 23 |
+
<style>
|
| 24 |
+
body {
|
| 25 |
+
background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
|
| 26 |
+
color: #FFFFFF;
|
| 27 |
+
}
|
| 28 |
+
h1, h2, h3 {
|
| 29 |
+
text-align: center;
|
| 30 |
+
color: #FFD700;
|
| 31 |
+
font-family: 'Poppins', sans-serif;
|
| 32 |
+
}
|
| 33 |
+
.footer {
|
| 34 |
+
position: fixed;
|
| 35 |
+
left: 0;
|
| 36 |
+
bottom: 0;
|
| 37 |
+
width: 100%;
|
| 38 |
+
color: white;
|
| 39 |
+
text-align: center;
|
| 40 |
+
padding: 10px;
|
| 41 |
+
background-color: rgba(0,0,0,0.4);
|
| 42 |
+
}
|
| 43 |
+
.stButton > button {
|
| 44 |
+
background-color: #FFD700 !important;
|
| 45 |
+
color: black !important;
|
| 46 |
+
font-weight: bold;
|
| 47 |
+
border-radius: 10px;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
| 50 |
+
""", unsafe_allow_html=True)
|
| 51 |
+
|
| 52 |
+
# -------------------#
|
| 53 |
+
# App Title
|
| 54 |
+
# -------------------#
|
| 55 |
+
st.markdown("<h1>๐งญ FutureScope: Research Direction Explorer</h1>", unsafe_allow_html=True)
|
| 56 |
+
st.markdown("<p style='text-align:center;'>Discover how your research area evolved and where it's heading next ๐</p>", unsafe_allow_html=True)
|
| 57 |
+
|
| 58 |
+
# -------------------#
|
| 59 |
+
# User Input
|
| 60 |
+
# -------------------#
|
| 61 |
+
user_topic = st.text_input("๐ Enter your research topic", placeholder="e.g. Graph Neural Networks for Drug Discovery")
|
| 62 |
+
|
| 63 |
+
# -------------------#
|
| 64 |
+
# Main Logic
|
| 65 |
+
# -------------------#
|
| 66 |
+
if st.button("Generate Research Insights"):
|
| 67 |
+
if not user_topic.strip():
|
| 68 |
+
st.warning("โ ๏ธ Please enter a valid research topic.")
|
| 69 |
+
else:
|
| 70 |
+
with st.spinner("Analyzing topic evolution and forecasting future directions... โณ"):
|
| 71 |
+
|
| 72 |
+
# Prompt Design
|
| 73 |
+
prompt = f"""
|
| 74 |
+
You are a world-class AI research assistant specialized in analyzing research trends.
|
| 75 |
+
Given the topic: "{user_topic}", perform the following:
|
| 76 |
+
1. Summarize how this research area has evolved in the past 10โ15 years.
|
| 77 |
+
2. Identify key milestones and subfields in a timeline format.
|
| 78 |
+
3. Predict 3โ5 future research directions and explain why each matters.
|
| 79 |
+
Return the output strictly in JSON format like this:
|
| 80 |
+
{{
|
| 81 |
+
"evolution_summary": "...",
|
| 82 |
+
"timeline": [{{"year": ..., "trend": "..."}}, ...],
|
| 83 |
+
"future_directions": [{{"title": "...", "reason": "..."}}, ...]
|
| 84 |
+
}}
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
# Call Together API
|
| 88 |
+
response = client.chat.completions.create(
|
| 89 |
+
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 90 |
+
messages=[{"role": "user", "content": prompt}]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
raw_content = response.choices[0].message.content
|
| 94 |
+
|
| 95 |
+
# -------------------#
|
| 96 |
+
# JSON Cleaning & Parsing
|
| 97 |
+
# -------------------#
|
| 98 |
+
def extract_json(text):
|
| 99 |
+
"""Extract valid JSON portion from the model response."""
|
| 100 |
+
text = text.strip()
|
| 101 |
+
text = re.sub(r"^```json|```$", "", text).strip() # remove code fences
|
| 102 |
+
match = re.search(r'\{.*\}', text, re.DOTALL)
|
| 103 |
+
if match:
|
| 104 |
+
return match.group(0)
|
| 105 |
+
return text
|
| 106 |
+
|
| 107 |
+
cleaned = extract_json(raw_content)
|
| 108 |
+
try:
|
| 109 |
+
data = json.loads(cleaned)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
st.error(f"โ ๏ธ Failed to parse JSON: {e}")
|
| 112 |
+
st.text_area("Raw Response", raw_content, height=300)
|
| 113 |
+
st.stop()
|
| 114 |
+
|
| 115 |
+
# -------------------#
|
| 116 |
+
# Display Results
|
| 117 |
+
# -------------------#
|
| 118 |
+
st.markdown("## ๐งฉ Evolution Summary")
|
| 119 |
+
st.markdown(f"<div style='background:#1e2a38;padding:15px;border-radius:10px;'>{data['evolution_summary']}</div>", unsafe_allow_html=True)
|
| 120 |
+
|
| 121 |
+
# Timeline Chart
|
| 122 |
+
if "timeline" in data and len(data["timeline"]) > 0:
|
| 123 |
+
df = pd.DataFrame(data["timeline"])
|
| 124 |
+
if "year" in df.columns and "trend" in df.columns:
|
| 125 |
+
fig = px.scatter(df, x="year", y="trend", title="๐ Topic Evolution Over Time",
|
| 126 |
+
size=[10]*len(df), text="trend", color_discrete_sequence=["gold"])
|
| 127 |
+
fig.update_traces(textposition='top center', marker=dict(symbol="circle"))
|
| 128 |
+
fig.update_layout(template="plotly_dark", height=500)
|
| 129 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 130 |
+
else:
|
| 131 |
+
st.warning("Timeline data invalid โ showing raw table:")
|
| 132 |
+
st.dataframe(df)
|
| 133 |
+
|
| 134 |
+
# Future Directions
|
| 135 |
+
st.markdown("## ๐ฎ Predicted Future Directions")
|
| 136 |
+
for item in data.get("future_directions", []):
|
| 137 |
+
st.markdown(f"""
|
| 138 |
+
<div style='background:#142733;padding:15px;margin:10px;border-radius:10px;'>
|
| 139 |
+
<h4>๐ง {item['title']}</h4>
|
| 140 |
+
<p>{item['reason']}</p>
|
| 141 |
+
</div>
|
| 142 |
+
""", unsafe_allow_html=True)
|
| 143 |
+
|
| 144 |
+
# Tools: Copy / Download
|
| 145 |
+
col1, col2 = st.columns(2)
|
| 146 |
+
with col1:
|
| 147 |
+
if st.button("๐ Copy Insights"):
|
| 148 |
+
st.write("Copied to clipboard! (Use Ctrl+C manually to copy)")
|
| 149 |
+
with col2:
|
| 150 |
+
st.download_button(
|
| 151 |
+
label="๐พ Download JSON",
|
| 152 |
+
data=json.dumps(data, indent=2),
|
| 153 |
+
file_name=f"{user_topic.replace(' ','_')}_future_directions.json",
|
| 154 |
+
mime="application/json"
|
| 155 |
+
)
|
| 156 |
|
| 157 |
+
# -------------------#
|
| 158 |
+
# Footer
|
| 159 |
+
# -------------------#
|
| 160 |
+
st.markdown("<div class='footer'>ยฉ Group 6 ILP TCS Research ", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|