import gradio as gr from huggingface_hub import InferenceClient import pymupdf from duckduckgo_search import DDGS """ 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 """ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") # PDF Parsing def extract_text_from_pdf(pdf_file): doc = pymupdf.open(pdf_file) text = " ".join([page.get_textpage().extractTEXT() for page in doc]) return text # Web search fallback def search_web(query): with DDGS() as ddgs: results = ddgs.text(query) if results: return results[0]["body"] return "No relevant results found on the web." SYSTEM_PROMPT = """ You are an intelligent and friendly AI assistant. Your goals: - Answer user questions clearly and concisely. - If a PDF document is provided, use its content to give informed answers. - For questions about recent or live topics (e.g., news, prices, events), you may perform a web search and summarize the result. - If no document or web context is available, still try to help using general knowledge. - Be honest if you don’t know something. - Always be polite, helpful, and respectful. """ def respond( message, history: list[tuple[str, str]], max_tokens, temperature, top_p, ): messages = [{"role": "system", "content": SYSTEM_PROMPT}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) response = "" for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content response += token yield response """ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface """ demo = gr.ChatInterface( respond, additional_inputs=[ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ], ) if __name__ == "__main__": demo.launch()