Shami96 commited on
Commit
1455b2b
·
verified ·
1 Parent(s): 7341307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -54
app.py CHANGED
@@ -1,64 +1,71 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
27
 
28
- response = ""
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
41
 
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ from langchain_community.vectorstores import Chroma
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings
7
+ from langchain_groq import ChatGroq
8
+ from langchain.document_loaders import PyPDFLoader
9
 
10
+ # --- Hugging Face Hub Setup ---
11
+ HF_REPO_ID = "Shami96/7solar_documentation" # Replace with your dataset
12
+ HF_PDF_NAME = "7solar_documentation.pdf" # Your PDF filename
 
13
 
14
+ # --- Load PDF from Hugging Face Hub ---
15
+ def load_pdf_from_hf():
16
+ pdf_path = hf_hub_download(
17
+ repo_id=HF_REPO_ID,
18
+ filename=HF_PDF_NAME,
19
+ token=os.environ.get("HF_TOKEN") # For private repos
20
+ )
21
+ loader = PyPDFLoader(pdf_path)
22
+ return loader.load()
23
 
24
+ # --- Split & Embed Docs ---
25
+ def create_vector_db():
26
+ docs = load_pdf_from_hf()
27
+ text_splitter = RecursiveCharacterTextSplitter(
28
+ chunk_size=2000,
29
+ chunk_overlap=300
30
+ )
31
+ chunks = text_splitter.split_documents(docs)
32
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
33
+ return Chroma.from_documents(chunks, embeddings)
34
 
35
+ # --- RAG Chatbot Logic ---
36
+ def get_response(query, history):
37
+ # Handle greetings
38
+ if query.lower() in ["hi", "hello", "hey"]:
39
+ return history + [(query, "Hello! 👋 Ask me about 7Solar's solar packages or services!")]
40
 
41
+ # Retrieve relevant doc chunks
42
+ matching_docs = vector_db.similarity_search(query, k=5)
43
+ if not matching_docs:
44
+ return history + [(query, "I couldn't find details. Ask about 7Solar's services!")]
45
 
46
+ # Generate LLM response
47
+ llm = ChatGroq(
48
+ model_name="llama3-70b-8192",
49
+ temperature=0.2,
50
+ api_key=os.environ.get("GROQ_API_KEY") # Set in Spaces Secrets
51
+ )
52
+ context = "\n\n".join([doc.page_content for doc in matching_docs])
53
+ response = llm.invoke(
54
+ f"Answer this query using ONLY the text below:\n\n{context}\n\nQuestion: {query}"
55
+ )
56
+ return history + [(query, response.content)]
57
 
58
+ # --- Initialize Vector DB ---
59
+ print("⚙️ Loading document...")
60
+ vector_db = create_vector_db()
 
 
 
 
 
61
 
62
+ # --- Gradio Interface ---
63
+ with gr.Blocks() as demo:
64
+ gr.Markdown("# ☀️ 7Solar Smart Assistant")
65
+ chatbot = gr.Chatbot()
66
+ msg = gr.Textbox(label="Ask about solar packages, services, etc.")
67
+ msg.submit(get_response, [msg, chatbot], [chatbot])
68
+ clear = gr.Button("Clear Chat")
69
+ clear.click(lambda: [], None, chatbot, queue=False)
70
 
71
+ demo.launch()