Himanshu2003 commited on
Commit
ae4e44e
·
verified ·
1 Parent(s): 5f18a79

Upload 4 files

Browse files
Files changed (4) hide show
  1. .env +1 -0
  2. app.py +29 -0
  3. main.py +96 -0
  4. requirements.txt +7 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GOOGLE_API_KEY = "AIzaSyA1_dEl52RWxEH197REEcDrYFvVlE5GCJU" # gemini
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import main
3
+
4
+ st.set_page_config(
5
+ page_title="Historical Event Explainer",
6
+ page_icon="🏛️",
7
+ layout="wide"
8
+ )
9
+
10
+ st.title("ChronoQuery: Explore History with AI 🕰️")
11
+ st.markdown("Unlock the past — ask about any historical event and get instant, AI-powered insights.")
12
+
13
+ with st.form(key="query_form"):
14
+ user_query = st.text_input("Enter a historical event or question:")
15
+ submit_button = st.form_submit_button(label="Submit")
16
+
17
+ if submit_button and user_query:
18
+ with st.spinner("⏳ Fetching historical insights..."):
19
+ try:
20
+ result = main.final_chain.invoke(user_query)
21
+ st.write(result)
22
+ except Exception as e:
23
+ st.error("⚠️ Something went wrong. Please try again.")
24
+ st.exception(e)
25
+
26
+
27
+
28
+
29
+
main.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.retrievers import WikipediaRetriever
2
+ from langchain_core.prompts import PromptTemplate
3
+ from langchain_core.output_parsers import StrOutputParser
4
+ from langchain_google_genai import ChatGoogleGenerativeAI
5
+ from langchain_community.tools import DuckDuckGoSearchRun
6
+ from langchain_core.tools import tool
7
+ from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
8
+ from dotenv import load_dotenv
9
+
10
+ # loading the environment key
11
+ load_dotenv()
12
+
13
+ # custom tool
14
+ @tool
15
+ def concat_data(docs : list , search_results: str) -> str:
16
+ """
17
+ takes all the page content from the docs and search_results. then combines them and returns them
18
+ """
19
+ data = ""
20
+ for doc in docs:
21
+ data += "\n\n" + doc.page_content
22
+
23
+ data += "\n\n" + search_results
24
+
25
+ return data
26
+
27
+ # model
28
+ model = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
29
+
30
+ # prompt template
31
+ main_template = PromptTemplate(
32
+ template = """
33
+ You are a historical assistant. Based on the following context, answer the user's question or just summarize the topic if it's not a question. Do not use * in the output. \n\n
34
+ Context:
35
+ {context}
36
+ \n\n\n
37
+ user's question:
38
+ {question}
39
+
40
+ """,
41
+ input_variables = ["context", "question"]
42
+ )
43
+
44
+ wiki_template = PromptTemplate(
45
+ template="""
46
+ You are an expert at identifying the core topic of a user's historical question.
47
+ Your task is to extract and return only the **specific topic or event** the query refers to — no extra explanation.
48
+
49
+ Examples:
50
+ - Query: who killed maharana pratap?
51
+ Output: maharana pratap
52
+
53
+ - Query: when did india china war happen?
54
+ Output: india china war
55
+
56
+ - Query: who caused world war 2?
57
+ Output: world war 2
58
+
59
+ Now do the same for the following:
60
+ Query: {query}
61
+ Output:""",
62
+ input_variables=["query"]
63
+ )
64
+
65
+ # output parser
66
+ parser = StrOutputParser()
67
+
68
+ # web search tool (built in tool)
69
+ search_tool = DuckDuckGoSearchRun()
70
+
71
+ # wikipedia retriever (built in tool)
72
+ retriever = WikipediaRetriever(top_k_results=4, lang="en")
73
+
74
+ # chains
75
+ data_chain = RunnableParallel({
76
+ 'docs': wiki_template | model | parser | retriever,
77
+ 'search_results': search_tool
78
+ })
79
+
80
+ question_chain = RunnablePassthrough()
81
+
82
+ mid_chain = RunnableParallel({
83
+ 'context' : data_chain | concat_data,
84
+ 'question' : question_chain
85
+ })
86
+
87
+ final_chain = mid_chain | main_template | model | parser
88
+
89
+ # query = "wars between china and india"
90
+ # output = final_chain.invoke(query)
91
+ # print(output)
92
+
93
+
94
+
95
+
96
+
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ langchain-community
2
+ langchain-google-genai
3
+ google-ai-generativelanguage
4
+ faiss-cpu
5
+ wikipedia
6
+ duckduckgo-search
7
+ streamlit