import requests from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, ServiceContext, PromptHelper import gradio as gr import os from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() # Get Hugging Face API key from environment variable hf_api_key = os.getenv("HF_API_KEY") API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3" headers = {"Authorization": f"Bearer {hf_api_key}"} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.json() def init_index(directory_path): # Model parameters max_input_size = 4096 num_outputs = 512 max_chunk_overlap = 20 chunk_size_limit = 600 # Prompt helper and predictor prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit) llm_predictor = LLMPredictor(llm=query) # Read documents from the "docs" folder documents = SimpleDirectoryReader(directory_path).load_data() # Initialize index with document data service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper) index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context) # Save the created index index.save_to_disk('index.json') return index def chatbot(input_text): # Load index index = GPTSimpleVectorIndex.load_from_disk('index.json') # Get response for the question response = index.query(input_text, response_mode="compact") return response.response # Create index init_index("docs") # Create UI interface to interact with the Hugging Face model iface = gr.Interface(fn=chatbot, inputs=gr.components.Textbox(lines=7, placeholder="Enter your question here"), outputs="text", title="Frost AI ChatBot: Your Knowledge Companion Powered by Hugging Face", description="Ask any question about rahasak research papers", allow_screenshot=True) iface.launch(share=True)