Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import gradio as grfrom huggingface_hub import InferenceClient"""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("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]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.contentresponse += tokenyield 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.Textbox(value="You are a friendly Chatbot.", label="System message"),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()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import osfrom getpass import getpassimport gradio as grpinecone_api_key = os.getenv("PINECONE_API_KEY") or getpass("Enter your Pinecone API Key: ")openai_api_key = os.getenv("OPENAI_API_KEY") or getpass("Enter your OpenAI API Key: ")from llama_index.node_parser import SemanticSplitterNodeParserfrom llama_index.embeddings import OpenAIEmbeddingfrom llama_index.ingestion import IngestionPipeline# This will be the model we use both for Node parsing and for vectorizationembed_model = OpenAIEmbedding(api_key=openai_api_key)# Define the initial pipelinepipeline = IngestionPipeline(transformations=[SemanticSplitterNodeParser(buffer_size=1,breakpoint_percentile_threshold=95,embed_model=embed_model,),embed_model,],)from pinecone.grpc import PineconeGRPCfrom pinecone import ServerlessSpecfrom llama_index.vector_stores import PineconeVectorStore# Initialize connection to Pineconepc = PineconeGRPC(api_key=pinecone_api_key)index_name = "anualreport"# Initialize your indexpinecone_index = pc.Index(index_name)# Initialize VectorStorevector_store = PineconeVectorStore(pinecone_index=pinecone_index)pinecone_index.describe_index_stats()from llama_index import VectorStoreIndexfrom llama_index.retrievers import VectorIndexRetriever# Due to how LlamaIndex works here, if your Open AI API key was# not set to an environment variable before, you have to set it at this pointif not os.getenv('OPENAI_API_KEY'):os.environ['OPENAI_API_KEY'] = openai_api_key# Instantiate VectorStoreIndex object from our vector_store objectvector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)# Grab 5 search resultsretriever = VectorIndexRetriever(index=vector_index, similarity_top_k=5)from llama_index.query_engine import RetrieverQueryEngine# Pass in your retriever from above, which is configured to return the top 5 resultsquery_engine = RetrieverQueryEngine(retriever=retriever)# Define the function to handle user input and return the query responsedef query_annual_report(summary_request):llm_query = query_engine.query(summary_request)return llm_query.response# Create the Gradio interfaceiface = gr.Interface(fn=query_annual_report,inputs="text",outputs="text",title="Annual Report Summary Query",description="Enter your query to get the summary of the annual report.")# Launch the Gradio appiface.launch()
app.py+75-60