Spaces:
Running
Running
File size: 12,277 Bytes
e44e7cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | # import os
# import sys
# import requests
# from langchain.chains import ConversationalRetrievalChain
# from langchain.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader
# from langchain_text_splitters import CharacterTextSplitter
# from langchain.vectorstores import Chroma
# from langchain.embeddings import HuggingFaceEmbeddings
# from langchain.llms.base import LLM
# import gradio as gr
# # workaround for sqlite in HF spaces
# __import__('pysqlite3')
# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
# # π Load documents
# docs = []
# for f in os.listdir("multiple_docs"):
# if f.endswith(".pdf"):
# loader = PyPDFLoader(os.path.join("multiple_docs", f))
# docs.extend(loader.load())
# elif f.endswith(".docx") or f.endswith(".doc"):
# loader = Docx2txtLoader(os.path.join("multiple_docs", f))
# docs.extend(loader.load())
# elif f.endswith(".txt"):
# loader = TextLoader(os.path.join("multiple_docs", f))
# docs.extend(loader.load())
# # π Split into chunks
# splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
# docs = splitter.split_documents(docs)
# texts = [doc.page_content for doc in docs]
# metadatas = [{"id": i} for i in range(len(texts))]
# # π§ Embeddings
# embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# # ποΈ Vectorstore
# vectorstore = Chroma(
# persist_directory="./db",
# embedding_function=embedding_function
# )
# vectorstore.add_texts(texts=texts, metadatas=metadatas)
# vectorstore.persist()
# # π Get DeepSeek API key from env
# DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
# if DEEPSEEK_API_KEY is None:
# raise ValueError("DEEPSEEK_API_KEY environment variable is not set.")
# # π DeepSeek API endpoint
# DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
# # π· Wrap DeepSeek API into LangChain LLM
# class DeepSeekLLM(LLM):
# """LLM that queries DeepSeek's API."""
# api_key: str = DEEPSEEK_API_KEY
# def _call(self, prompt, stop=None, run_manager=None, **kwargs):
# headers = {
# "Authorization": f"Bearer {self.api_key}",
# "Content-Type": "application/json"
# }
# payload = {
# "model": "deepseek-chat", # adjust if you have a specific model name
# "messages": [
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": prompt}
# ],
# "temperature": 0.7,
# "max_tokens": 512
# }
# response = requests.post(DEEPSEEK_API_URL, headers=headers, json=payload)
# response.raise_for_status()
# data = response.json()
# return data["choices"][0]["message"]["content"].strip()
# @property
# def _llm_type(self) -> str:
# return "deepseek_api"
# llm = DeepSeekLLM()
# # π Conversational chain
# chain = ConversationalRetrievalChain.from_llm(
# llm,
# retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
# return_source_documents=True,
# verbose=False
# )
# # π¬ Gradio UI
# chat_history = []
# with gr.Blocks() as demo:
# chatbot = gr.Chatbot(
# [("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my experience, where I'm eligible to work, skills etc you can chat with me directly in multiple languages")],
# avatar_images=["./multiple_docs/Guest.jpg", "./multiple_docs/Thierry Picture.jpg"]
# )
# msg = gr.Textbox(placeholder="Type your question here...")
# clear = gr.Button("Clear")
# def user(query, chat_history):
# chat_history_tuples = [(m[0], m[1]) for m in chat_history]
# result = chain({"question": query, "chat_history": chat_history_tuples})
# chat_history.append((query, result["answer"]))
# return gr.update(value=""), chat_history
# msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
# clear.click(lambda: None, None, chatbot, queue=False)
# demo.launch(debug=True) # remove share=True if running in HF Spaces
import os
import sys
from langchain_classic.chains import ConversationalRetrievalChain, LLMChain
from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain_classic.chains.question_answering import load_qa_chain
import gradio as gr
# workaround for sqlite in HF spaces (only needed on HuggingFace Spaces)
try:
__import__('pysqlite3')
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
except ImportError:
# pysqlite3 not available, use system sqlite3 (fine for local macOS/Linux)
pass
# π Load documents
docs = []
for f in os.listdir("multiple_docs"):
if f.endswith(".pdf"):
loader = PyPDFLoader(os.path.join("multiple_docs", f))
docs.extend(loader.load())
elif f.endswith(".docx") or f.endswith(".doc"):
loader = Docx2txtLoader(os.path.join("multiple_docs", f))
docs.extend(loader.load())
elif f.endswith(".txt"):
loader = TextLoader(os.path.join("multiple_docs", f))
docs.extend(loader.load())
# π Split into chunks
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
docs = splitter.split_documents(docs)
# π§ Embeddings
embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# ποΈ Vectorstore - use from_documents which is more modern
vectorstore = Chroma.from_documents(
documents=docs,
embedding=embedding_function,
persist_directory="./db"
)
# π€ Use HuggingFace Inference API (hosted in Spaces)
# HF token (must be set via HF_TOKEN environment variable)
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
raise ValueError("HF_TOKEN environment variable is not set. Please set it before running the application.")
# You can change MODEL_NAME to use different models from HuggingFace:
# Popular options:
# - "microsoft/DialoGPT-large" (conversational model)
# - "HuggingFaceH4/zephyr-7b-beta" (high quality chat model)
# - "mistralai/Mistral-7B-Instruct-v0.2" (excellent quality)
# - "meta-llama/Llama-2-7b-chat-hf" (if you have access)
# - "google/flan-t5-large" (good for Q&A)
MODEL_NAME = os.getenv("HF_MODEL_NAME", "meta-llama/Llama-3.1-8B-Instruct:novita")
print(f"Using HuggingFace Router API with model: {MODEL_NAME}")
# Initialize ChatOpenAI with HuggingFace Router (OpenAI-compatible API)
# This allows using provider suffixes like :novita
# Uses OpenAI-compatible interface via HF router
llm = ChatOpenAI(
model=MODEL_NAME,
base_url="https://router.huggingface.co/v1",
api_key=HF_TOKEN,
temperature=0.7,
max_tokens=512,
)
# β¨ Custom prompt template
template = """
You are an AI trading advisor for the Quantum Financial Network trading universe.
IMPORTANT: You are providing DIRECT TRADING RECOMMENDATIONS. Give clear, actionable advice based on:
- Current market scenarios described in the question
- Risk tolerance level specified (0=conservative, 10=aggressive)
- Company information and market context provided
- Specific stock symbols and prices mentioned
When asked about a specific trade (e.g., "Should I buy X shares of Y?"), you MUST:
1. Give a CLEAR RECOMMENDATION: "Yes, buy..." or "No, don't buy..." or "Consider buying/selling..."
2. Explain WHY based on the current scenario and company information
3. Reference specific companies, market events, or data from the context
4. Adjust your certainty based on the risk tolerance level mentioned
5. Be DIRECT and ACTIONABLE - no vague "I'd need more information" responses
Use the following context to answer the user's question. Always give a direct recommendation.
Context:
{context}
Question: {question}
Answer:
"""
prompt = PromptTemplate(
input_variables=["context", "question"],
template=template,
)
# π QA chain with custom prompt
qa_chain = load_qa_chain(llm, chain_type="stuff", prompt=prompt)
# π· Question rephraser chain for follow-up questions β standalone
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(
"""
Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:
"""
)
question_generator = LLMChain(
llm=llm,
prompt=CONDENSE_QUESTION_PROMPT
)
# π· Finally: build the ConversationalRetrievalChain manually
chain = ConversationalRetrievalChain(
retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
question_generator=question_generator,
combine_docs_chain=qa_chain,
return_source_documents=True,
verbose=False
)
# π¬ Gradio UI
chat_history = []
with gr.Blocks() as demo:
chatbot = gr.Chatbot(
value=[{"role": "assistant", "content": "Welcome to the Quantum Financial Network! I'm your trading universe assistant. You can ask me about companies (VCG, CSI, STDY, AUBIO, NLN), market events, trading strategies, currency systems, trading hours, market history, and anything about our financial markets. What would you like to know?"}]
)
msg = gr.Textbox(placeholder="Type your question here...")
clear = gr.Button("Clear")
def user(query, chat_history):
# Convert Gradio 4.x format (dicts) to tuple format for LangChain chain
# LangChain expects list of tuples: [(human_message, ai_message), ...]
chat_history_tuples = []
current_human = None
current_ai = None
for msg in chat_history:
if isinstance(msg, dict):
role = msg.get("role", "")
content = msg.get("content", "")
if role == "user":
# If we have a previous human message without AI response, add it with empty AI
if current_human is not None:
human_str = str(current_human) if current_human else ""
ai_str = str(current_ai) if current_ai else ""
chat_history_tuples.append((human_str, ai_str))
current_human = str(content) if content else ""
current_ai = None
elif role == "assistant":
current_ai = str(content) if content else ""
# Pair the human and AI messages
if current_human is not None:
human_str = str(current_human) if current_human else ""
ai_str = str(current_ai) if current_ai else ""
chat_history_tuples.append((human_str, ai_str))
current_human = None
current_ai = None
else:
# Fallback for old tuple format
if isinstance(msg, (list, tuple)) and len(msg) >= 2:
chat_history_tuples.append((str(msg[0]), str(msg[1])))
# Handle case where last message was human without AI response
if current_human is not None:
human_str = str(current_human) if current_human else ""
ai_str = str(current_ai) if current_ai else ""
chat_history_tuples.append((human_str, ai_str))
# Get response from chain
result = chain({"question": query, "chat_history": chat_history_tuples})
# Append both user query and assistant response in Gradio 4.x format
chat_history.append({"role": "user", "content": query})
chat_history.append({"role": "assistant", "content": result["answer"]})
return gr.update(value=""), chat_history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
clear.click(lambda: None, None, chatbot, queue=False)
# Only launch Gradio demo if running this file directly (not when imported)
if __name__ == "__main__":
demo.launch(debug=True) # remove share=True if running in HF Spaces
|