Spaces:
Runtime error
Runtime error
Update app.py
Browse files12345648 unmodified lines555657585960614 unmodified lines66676869707172737475767778791 unmodified line8182838485868788892 unmodified lines92939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411 unmodified line1431441451461471481491501511521531541551561571581591603 unmodified lines1641651661671681691701711721733 unmodified lines17717817918018118218318418518618718818919019119220 unmodified lines21321421521621721821922022122222322422522622722839 unmodified lines268269270271import osimport loggingimport asyncioimport gradio as grfrom huggingface_hub import InferenceClientfrom langchain.embeddings import HuggingFaceEmbeddings48 unmodified lineslogging.info(f"Created vectors for {len(documents)} search results.")return FAISS.from_documents(documents, embed)async def get_response_with_search(query, system_prompt, model, use_embeddings, history=None, num_calls=3, temperature=0.2):search_results = duckduckgo_search(query)if not search_results:4 unmodified linessources = [result['href'] for result in search_results if 'href' in result]source_list_str = "\n".join(sources)if use_embeddings:web_search_database = create_web_search_vectors(search_results)retriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)context = "\n".join([doc.page_content for doc in relevant_docs])else:context = "\n".join([f"{result['title']}\n{result['body']}" for result in search_results])logging.info(f"Context created for query: {query}")user_message = f"""Using the following context from web search results:1 unmodified lineWrite a detailed and complete research document that fulfills the following user request: '{query}'."""client = InferenceClient(model, token=huggingface_token)full_response = ""messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": user_message}2 unmodified linesif history:messages = history + messagestry:for call in range(num_calls):try:response_stream = client.chat_completion(messages=messages,max_tokens=6000,temperature=temperature,stream=True,top_p=0.8,)if response_stream is None:logging.error(f"API call {call + 1} returned None")yield "The API returned an empty response. Please try again.", ""continuefor response in response_stream:if isinstance(response, dict) and "choices" in response:for choice in response["choices"]:if "delta" in choice and "content" in choice["delta"]:chunk = choice["delta"]["content"]full_response += chunkyield full_response, ""else:logging.error(f"Unexpected response format in API call {call + 1}: {response}")if full_response:break # If we got a valid response, exit the loopexcept Exception as e:logging.error(f"Error in API call {call + 1}: {str(e)}")if "422 Client Error" in str(e):logging.warning("Received 422 Client Error. Adjusting request parameters.")# You might want to adjust parameters here, e.g., reduce max_tokensyield f"An error occurred during API call {call + 1}. Retrying...", ""await asyncio.sleep(1) # 1 second delay between callsexcept asyncio.CancelledError:logging.warning("The operation was cancelled.")yield "The operation was cancelled. Please try again.", ""except Exception as e:logging.error(f"Unexpected error in get_response_with_search: {str(e)}")yield f"An unexpected error occurred: {str(e)}", ""if not full_response:logging.warning("No response generated from the model")1 unmodified lineelse:yield f"{full_response}\n\nSources:\n{source_list_str}", ""async def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Temperature: {temperature}")logging.info(f"Number of API Calls: {num_calls}")logging.info(f"Use Embeddings: {use_embeddings}")logging.info(f"System Prompt: {system_prompt}")logging.info(f"History: {history}") # Log the history for debugging# Convert gradio history to the format expected by get_response_with_searchchat_history = []if history:for entry in history:if isinstance(entry, (list, tuple)) and len(entry) == 2:human, assistant = entry3 unmodified lineselif isinstance(entry, str):# If it's a string, assume it's a user messagechat_history.append({"role": "user", "content": entry})# Ignore any other formatstry:full_response = ""async for main_content, sources in get_response_with_search(message,system_prompt,3 unmodified linesnum_calls=num_calls,temperature=temperature):if "error" in main_content.lower() or "no response" in main_content.lower():# If it's an error message, yield it as isyield main_contentelse:# Otherwise, yield only the new contentnew_content = main_content[len(full_response):]full_response = main_contentyield new_content# Yield the sources as a separate messageif sources:yield f"\n\nSources:\n{sources}"20 unmodified linesdef create_gradio_interface():custom_placeholder = "Enter your question here for web search."async def wrapped_respond(*args):try:async for response in respond(*args):yield responseexcept Exception as e:logging.error(f"Error in wrapped_respond: {str(e)}")yield f"An error occurred: {str(e)}"demo = gr.ChatInterface(fn=wrapped_respond, # Use the wrapped versionadditional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),additional_inputs=[gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, lines=6, label="System Prompt", placeholder="Enter your system prompt here"),39 unmodified linesif __name__ == "__main__":demo = create_gradio_interface()demo.launch(share=True)123456748 unmodified lines565758596061626364656667686970714 unmodified lines767778798081821 unmodified line8485868788892 unmodified lines92939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291 unmodified line1311321331341351361371381391401411423 unmodified lines1461471481491501511521531541551561571581591601611621631641653 unmodified lines16917017117217317417517620 unmodified lines19719819920020120220320439 unmodified lines244245246247import osimport loggingimport asynciofrom typing import AsyncGenerator, Tupleimport gradio as grfrom huggingface_hub import InferenceClientfrom langchain.embeddings import HuggingFaceEmbeddings48 unmodified lineslogging.info(f"Created vectors for {len(documents)} search results.")return FAISS.from_documents(documents, embed)def create_context(search_results, use_embeddings, query):if use_embeddings:web_search_database = create_web_search_vectors(search_results)retriever = web_search_database.as_retriever(search_kwargs={"k": 5})relevant_docs = retriever.get_relevant_documents(query)return "\n".join([doc.page_content for doc in relevant_docs])else:return "\n".join([f"{result['title']}\n{result['body']}" for result in search_results])async def get_response_with_search(query: str, system_prompt: str, model: str, use_embeddings: bool, history=None, num_calls: int = 3, temperature: float = 0.2) -> AsyncGenerator[Tuple[str, str], None]:search_results = duckduckgo_search(query)if not search_results:4 unmodified linessources = [result['href'] for result in search_results if 'href' in result]source_list_str = "\n".join(sources)context = create_context(search_results, use_embeddings, query)logging.info(f"Context created for query: {query}")user_message = f"""Using the following context from web search results:1 unmodified lineWrite a detailed and complete research document that fulfills the following user request: '{query}'."""messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": user_message}2 unmodified linesif history:messages = history + messagesclient = InferenceClient(model, token=huggingface_token)full_response = ""for call in range(num_calls):try:response = await asyncio.to_thread(client.chat_completion,messages=messages,max_tokens=6000,temperature=temperature,top_p=0.8,)if response is None or not isinstance(response, dict) or 'choices' not in response:logging.error(f"API call {call + 1} returned an invalid response: {response}")if call == num_calls - 1:yield "The API returned an invalid response. Please try again later.", ""continuenew_content = response['choices'][0]['message']['content']full_response += new_contentyield full_response, ""if full_response:break # If we got a valid response, exit the loopexcept Exception as e:logging.error(f"Error in API call {call + 1}: {str(e)}")if call == num_calls - 1:yield f"An error occurred during API calls: {str(e)}. Please try again later.", ""await asyncio.sleep(1) # 1 second delay between callsif not full_response:logging.warning("No response generated from the model")1 unmodified lineelse:yield f"{full_response}\n\nSources:\n{source_list_str}", ""def process_history(history):chat_history = []if isinstance(history, str):# If history is a string (like the system prompt), add it as a system messagechat_history.append({"role": "system", "content": history})elif isinstance(history, list):for entry in history:if isinstance(entry, (list, tuple)) and len(entry) == 2:human, assistant = entry3 unmodified lineselif isinstance(entry, str):# If it's a string, assume it's a user messagechat_history.append({"role": "user", "content": entry})return chat_historyasync def respond(message, system_prompt, history, model, temperature, num_calls, use_embeddings):logging.info(f"User Query: {message}")logging.info(f"Model Used: {model}")logging.info(f"Temperature: {temperature}")logging.info(f"Number of API Calls: {num_calls}")logging.info(f"Use Embeddings: {use_embeddings}")logging.info(f"System Prompt: {system_prompt}")logging.info(f"History: {history}")chat_history = process_history(history)try:async for main_content, sources in get_response_with_search(message,system_prompt,3 unmodified linesnum_calls=num_calls,temperature=temperature):yield main_contentif sources:yield f"\n\nSources:\n{sources}"20 unmodified linesdef create_gradio_interface():custom_placeholder = "Enter your question here for web search."demo = gr.ChatInterface(fn=respond,additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=True, render=False),additional_inputs=[gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, lines=6, label="System Prompt", placeholder="Enter your system prompt here"),39 unmodified linesif __name__ == "__main__":demo = create_gradio_interface()demo.launch(share=True)
app.py+64-88