vivekreddy1105 commited on
Commit
84bc3c0
Β·
verified Β·
1 Parent(s): d804be5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain.document_loaders import WebBaseLoader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.embeddings import HuggingFaceEmbeddings
7
+ from langchain.chains import RetrievalQA
8
+ from langchain_together import Together
9
+
10
+ # πŸ” Set your API key
11
+ os.environ["TOGETHER_API_KEY"] = os.environ.get("TOGETHER_API_KEY", "")
12
+
13
+
14
+ # πŸ” Caches
15
+ qa_cache = {}
16
+ retriever_cache = {}
17
+
18
+ # πŸ” Load and embed the website content
19
+ def load_url(url):
20
+ try:
21
+ loader = WebBaseLoader(url)
22
+ docs = loader.load()
23
+ splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
24
+ chunks = splitter.split_documents(docs)
25
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
26
+ db = FAISS.from_documents(chunks, embedding=embeddings)
27
+ retriever = db.as_retriever()
28
+ llm = Together(
29
+ model="mistralai/Mistral-7B-Instruct-v0.2",
30
+ temperature=0.5,
31
+ max_tokens=512
32
+ )
33
+ qa = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
34
+ return retriever, qa, "βœ… Website loaded. You can start chatting!"
35
+ except Exception as e:
36
+ return None, None, f"❌ Error: {str(e)}"
37
+
38
+ # πŸ’¬ Chat handler
39
+ def chat(message, history, url):
40
+ if url not in qa_cache:
41
+ retriever, qa, status = load_url(url)
42
+ if retriever is None:
43
+ history.append({"role": "user", "content": message})
44
+ history.append({"role": "assistant", "content": status})
45
+ return history, ""
46
+ retriever_cache[url] = retriever
47
+ qa_cache[url] = qa
48
+ history.append({"role": "system", "content": status})
49
+ else:
50
+ qa = qa_cache[url]
51
+
52
+ try:
53
+ result = qa.invoke({"query": message})["result"]
54
+ except Exception as e:
55
+ result = f"❌ Error: {str(e)}"
56
+
57
+ history.append({"role": "user", "content": message})
58
+ history.append({"role": "assistant", "content": result})
59
+ return history, ""
60
+
61
+ # βœ… Gradio UI
62
+ with gr.Blocks() as demo:
63
+ gr.Markdown("## 🧠 Chat with Any Website")
64
+
65
+ url_input = gr.Textbox(label="Website URL", placeholder="https://en.wikipedia.org/wiki/LangChain")
66
+ chatbot = gr.Chatbot(label="Chat", type="messages")
67
+ msg_input = gr.Textbox(show_label=False, placeholder="Ask your question here and press Enter...")
68
+ state = gr.State([])
69
+
70
+ msg_input.submit(chat, inputs=[msg_input, state, url_input], outputs=[chatbot, msg_input])
71
+
72
+ # πŸ‘‡ Footer
73
+ gr.Markdown(
74
+ """
75
+ ---
76
+ <center>
77
+ πŸ”— <a href="https://github.com/vivekreddy1105" target="_blank">GitHub</a> |
78
+ πŸ’Ό <a href="https://www.linkedin.com/in/vivekreddy1105/" target="_blank">LinkedIn</a><br>
79
+ Β© 2025 Vivek Reddy Eluka
80
+ </center>
81
+ """,
82
+ elem_id="footer"
83
+ )
84
+
85
+ demo.launch(share=True)