|
import requests |
|
from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, ServiceContext, PromptHelper |
|
import gradio as gr |
|
|
|
import os |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
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): |
|
|
|
max_input_size = 4096 |
|
num_outputs = 512 |
|
max_chunk_overlap = 20 |
|
chunk_size_limit = 600 |
|
|
|
|
|
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit) |
|
llm_predictor = LLMPredictor(llm=query) |
|
|
|
|
|
documents = SimpleDirectoryReader(directory_path).load_data() |
|
|
|
|
|
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper) |
|
index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context) |
|
|
|
|
|
index.save_to_disk('index.json') |
|
|
|
return index |
|
|
|
def chatbot(input_text): |
|
|
|
index = GPTSimpleVectorIndex.load_from_disk('index.json') |
|
|
|
|
|
response = index.query(input_text, response_mode="compact") |
|
|
|
return response.response |
|
|
|
|
|
init_index("docs") |
|
|
|
|
|
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) |
|
|