brianjorden commited on
Commit
e4ded93
·
verified ·
1 Parent(s): b28e6fb

Upload folder using huggingface_hub

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. .gitignore +2 -0
  3. README.md +6 -5
  4. app.py +107 -42
  5. faiss_index/index.faiss +3 -0
  6. faiss_index/index.pkl +3 -0
  7. requirements.txt +9 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ faiss_index/index.faiss filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .env
2
+ __pycache__/
README.md CHANGED
@@ -1,16 +1,17 @@
1
  ---
2
  title: Deater Chat
3
- emoji: 💬
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.42.0
8
  app_file: app.py
9
  pinned: false
10
- hf_oauth: true
11
- hf_oauth_scopes:
12
- - inference-api
13
  license: mit
14
  ---
15
 
16
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
1
  ---
2
  title: Deater Chat
3
+ emoji: 🧬
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.42.0
8
  app_file: app.py
9
  pinned: false
 
 
 
10
  license: mit
11
  ---
12
 
13
+ # HSAN1 Research Assistant
14
+
15
+ A RAG-powered chatbot that helps patients and families understand HSAN1 (Hereditary Sensory and Autonomic Neuropathy Type 1) using 246 research documents.
16
+
17
+ Built with [Gradio](https://gradio.app), [LangChain](https://langchain.com), and Google Gemini.
app.py CHANGED
@@ -1,70 +1,135 @@
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
 
5
  def respond(
6
  message,
7
  history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
  """
15
- 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
 
16
  """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
 
 
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
20
 
21
- messages.extend(history)
 
22
 
23
- messages.append({"role": "user", "content": message})
24
 
25
- response = ""
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = 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
  chatbot = gr.ChatInterface(
47
  respond,
48
  type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
  ],
 
61
  )
62
 
 
63
  with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
  chatbot.render()
67
 
68
-
69
  if __name__ == "__main__":
70
  demo.launch()
 
1
+ """
2
+ HSAN1 Research Assistant - Gradio Chat Interface with RAG
3
+ """
4
+ import os
5
  import gradio as gr
6
+ from dotenv import load_dotenv
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+ from langchain_huggingface import HuggingFaceEmbeddings
9
+ from langchain_community.vectorstores import FAISS
10
+ from langchain_core.messages import HumanMessage, SystemMessage
11
+
12
+ # Load environment variables (for local development)
13
+ load_dotenv()
14
+
15
+ # Configuration
16
+ INDEX_PATH = "./faiss_index"
17
+ SYSTEM_PROMPT = """You are a compassionate medical research assistant helping patients and families understand HSAN1 (Hereditary Sensory and Autonomic Neuropathy Type 1).
18
+
19
+ You have access to a database of 246 research documents including papers, newsletters, and family histories.
20
+
21
+ Instructions:
22
+ - Answer questions based ONLY on the provided context
23
+ - If the answer is not in the context, say "I don't see that information in the research documents I have."
24
+ - Use clear, empathetic language and explain medical terms
25
+ - Be accurate but hopeful in tone
26
+ - Keep responses concise but informative"""
27
+
28
+ # Check for API key
29
+ api_key = os.environ.get("GOOGLE_API_KEY")
30
+ if not api_key:
31
+ raise ValueError("GOOGLE_API_KEY environment variable not set")
32
+
33
+ # Load components at startup
34
+ print("Loading embeddings model...")
35
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
36
+
37
+ print("Loading FAISS index...")
38
+ if not os.path.exists(INDEX_PATH):
39
+ raise FileNotFoundError(f"FAISS index not found at {INDEX_PATH}. Run build_index.py first.")
40
+ vectorstore = FAISS.load_local(INDEX_PATH, embeddings, allow_dangerous_deserialization=True)
41
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
42
+
43
+ print("Initializing Gemini...")
44
+ llm = ChatGoogleGenerativeAI(
45
+ model="gemini-3-flash-preview",
46
+ temperature=0.3,
47
+ streaming=True
48
+ )
49
+
50
+ print("Ready!")
51
 
52
 
53
  def respond(
54
  message,
55
  history: list[dict[str, str]],
 
 
 
 
 
56
  ):
57
  """
58
+ Process a user message and generate a response using RAG.
59
+ Uses OpenAI-style message format for Gradio ChatInterface.
60
  """
61
+ # Retrieve relevant documents
62
+ docs = retriever.invoke(message)
63
+ context = "\n\n---\n\n".join([doc.page_content for doc in docs])
64
 
65
+ # Get unique sources
66
+ sources = list(set([
67
+ os.path.basename(doc.metadata.get("source", "Unknown"))
68
+ for doc in docs
69
+ ]))
70
 
71
+ # Build the prompt with context
72
+ augmented_prompt = f"""Context from research documents:
73
 
74
+ {context}
75
 
76
+ ---
77
 
78
+ User question: {message}
 
 
 
 
 
 
 
 
 
 
79
 
80
+ Please answer based on the context above."""
 
81
 
82
+ # Build messages for the LLM
83
+ messages = [
84
+ SystemMessage(content=SYSTEM_PROMPT),
85
+ HumanMessage(content=augmented_prompt)
86
+ ]
87
 
88
+ # Stream the response
89
+ response = ""
90
+ for chunk in llm.stream(messages):
91
+ if chunk.content:
92
+ content = chunk.content
93
+ # Handle different content formats from Gemini models
94
+ if isinstance(content, list):
95
+ # Extract text from list of content blocks
96
+ text_parts = []
97
+ for item in content:
98
+ if isinstance(item, dict) and 'text' in item:
99
+ text_parts.append(item['text'])
100
+ elif isinstance(item, str):
101
+ text_parts.append(item)
102
+ content = "".join(text_parts)
103
+ elif isinstance(content, dict) and 'text' in content:
104
+ content = content['text']
105
+ response += content
106
+ yield response
107
+
108
+ # Append sources after streaming completes
109
+ if sources:
110
+ source_text = f"\n\n---\n*Sources: {', '.join(sources[:3])}*"
111
+ yield response + source_text
112
+
113
+
114
+ # Create Gradio ChatInterface (matching HF template format)
115
  chatbot = gr.ChatInterface(
116
  respond,
117
  type="messages",
118
+ title="🧬 HSAN1 Research Assistant",
119
+ description="Ask questions about HSAN1 research, treatments, clinical trials, and more. Powered by 246 research documents.",
120
+ examples=[
121
+ "What is HSAN1?",
122
+ "What causes HSAN1?",
123
+ "Tell me about the L-serine treatment",
124
+ "What are the symptoms of HSAN1?",
125
+ "Is there a clinical trial for HSAN1?",
 
 
 
126
  ],
127
+ theme="soft",
128
  )
129
 
130
+ # Wrap in Blocks for proper rendering
131
  with gr.Blocks() as demo:
 
 
132
  chatbot.render()
133
 
 
134
  if __name__ == "__main__":
135
  demo.launch()
faiss_index/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75bcc650fcffe2e8e7c34cf6f3a1f643839bcffaec3286542f187c1df45dd204
3
+ size 2648109
faiss_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:686886b4f39a31cdd43b6e9e690c4b4bcc35692cb1d1cb5068a7811991f475af
3
+ size 1532857
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ gradio>=5.0.0
2
+ langchain>=1.2.0
3
+ langchain-community>=0.4.1
4
+ langchain-google-genai>=4.1.2
5
+ langchain-huggingface>=0.1.2
6
+ langchain-text-splitters>=1.1.0
7
+ sentence-transformers>=3.3.0
8
+ faiss-cpu>=1.13.2
9
+ python-dotenv>=1.0.1