import gradio as gr from gradio_client import Client, handle_file import os # Define your Hugging Face token (make sure to set it as an environment variable) HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token if not using an environment variable # Initialize the Gradio Client for the specified API #client = Client("on1onmangoes/CNIHUB10724v10", hf_token=HF_TOKEN) client = Client("on1onmangoes/CNIHUB101324v10", hf_token=HF_TOKEN) # on1onmangoes/CNIHUB101324v10 # Here's how you can fix it: # Update the conversation history within the function. # Return the updated history along with any other required outputs. def stream_chat_with_rag( message: str, history: list, client_name: str, system_prompt: str, num_retrieved_docs: int = 10, num_docs_final: int = 9, temperature: float = 0, max_new_tokens: int = 1024, top_p: float = 1.0, top_k: int = 20, penalty: float = 1.2, ): print(f"Message: {message}") print(f"History: {history}") # Build the conversation prompt including system prompt and history conversation = f"{system_prompt}\n\nFor Client: {client_name}\n" # Add previous conversation history for user_input, assistant_response in history: conversation += f"User: {user_input}\nAssistant: {assistant_response}\n" # Add the current user message conversation += f"User: {message}\nAssistant:" # Call the API with the user's message question = message answer = client.predict(question=question, api_name="/answer_with_rag") # Debugging: Print the raw response print("Raw answer from API:") print(answer) # Format the assistant's answer and the relevant documents separately formatted_answer = format_answer_string(answer) # Update the conversation history with the new message and answer history.append((message, formatted_answer)) # Return the formatted answer return formatted_answer def format_answer_string(answer: str): """ This function takes the full string returned by the API (which includes both the assistant's answer and documents) and splits it into separate sections: the assistant's answer and the relevant documents. """ # Step 1: Split the answer by the point where document context starts split_marker = "Extracted Documents with Descriptive Context:" if split_marker in answer: assistant_answer, documents_section = answer.split(split_marker, 1) else: # If no documents found, return the whole answer as the assistant's response return f"Assistant: {answer.strip()}" # Step 2: Clean and format the assistant's answer formatted_answer = f"Assistant: {assistant_answer.strip()}\n\n" # Step 3: Format the documents section formatted_answer += "Relevant Documents:\n" document_entries = documents_section.split("Document ") # Step 4: Reformat each document entry for entry in document_entries[1:]: # Skip the first empty split doc_number, rest = entry.split(":", 1) formatted_answer += f"{doc_number}. {rest.strip()}\n\n" return formatted_answer # this version works just issue with formatting # def stream_chat_with_rag( # message: str, # history: list, # client_name: str, # system_prompt: str, # num_retrieved_docs: int = 10, # num_docs_final: int = 9, # temperature: float = 0, # max_new_tokens: int = 1024, # top_p: float = 1.0, # top_k: int = 20, # penalty: float = 1.2, # ): # print(f"Message: {message}") # print(f"History: {history}") # # Build the conversation prompt including system prompt and history # conversation = system_prompt + "\n\n" + "For Client:" + client_name # for user_input, assistant_response in history: # conversation += f"User: {user_input}\nAssistant: {assistant_response}\n" # conversation += f"User: {message}\nAssistant:" # question = message # answer = client.predict(question=question, api_name="/answer_with_rag") # # debug 092624 # print("The Answer in stream_chat_with_rag:") # print(answer) # # Update the conversation history # history.append((message, answer)) # return answer # Function to handle PDF processing API call def process_pdf(pdf_file): return client.predict( pdf_file=handle_file(pdf_file), client_name="rosariarossi", # Hardcoded client name api_name="/process_pdf2" )[1] # Return only the result string # Function to handle search API call def search_api(query): return client.predict(query=query, api_name="/search_with_confidence") # Function to handle RAG API call def rag_api(question): return client.predict(question=question, api_name="/answer_with_rag") # CSS for custom styling CSS = """ # chat-container { height: 100vh; } """ # Title for the application TITLE = "