#import json import os import pprint #import shutil #import requests import gradio as gr from transformers.utils import logging from langchain.embeddings import HuggingFaceInstructEmbeddings, GooglePalmEmbeddings import pinecone from langchain.vectorstores import Pinecone logging.set_verbosity_debug() instructor_embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl", model_kwargs={"device": "cpu"}) HF_TOKEN = os.environ.get("HF_TOKEN", None) PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY", None) PINECONE_ENV = os.environ.get("PINECONE_ENV", None) GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", None) pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENV) from langchain.llms import GooglePalm from langchain.chains import RetrievalQAWithSourcesChain llm=GooglePalm(google_api_key=GOOGLE_API_KEY, temperature=0.1, max_output_tokens=2048) vectorStore = Pinecone.from_existing_index('macci', instructor_embeddings) retriever = vectorStore.as_retriever(search_kwargs={"k": 3}) qa_chain_instrucEmbed = RetrievalQAWithSourcesChain.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True, verbose=True ) theme = gr.themes.Monochrome( primary_hue="indigo", secondary_hue="blue", neutral_hue="slate", radius_size=gr.themes.sizes.radius_sm, font=[ gr.themes.GoogleFont("Open Sans"), "ui-sans-serif", "system-ui", "sans-serif", ], ) def generate(question): ret = qa_chain_instrucEmbed(question) pprint.pprint(ret) answer = ret['answer'] sources = ret['sources'] embed_video_html = '
' if sources is not None and len(sources) > 0: sources = [s.strip() for s in sources.split(',')] for source in sources: embed_video_html += f''' ''' return answer, embed_video_html+'
' examples = [ "Describe Serena Williams game style in details.", "What should I do to improve my forehand groundstroke? Describe the motions step by step.", "Compare Serena and Venus game style in details. Who is better?", "Compare Novak and Nadal gamestyle in details. Who is better?", "Who is the tennis GOAT?", "Who in the young generation will be next great tennis player? Explain in details.", "Which American tennis player will win a grand slam in the future?", "Can you help me improve my two handed backhand? I want to hit the balls with more spin and power.", "How should I coach a junior tennis player to be next Serena?", "What is mental toughness? Explain in details.", "How can I train mental toughness?" ] def process_example(args): for x in generate(args): pass return x css = ".generating {visibility: hidden}" monospace_css = """ #q-input textarea { font-family: monospace, 'Consolas', Courier, monospace; } """ css += monospace_css + ".gradio-container {color: black}" description = """

Ask Tennis Coach Rick Macci

This is a demo to answer some popular questions from tennis fans to Coach Rick. The information is being extracted from his official Youtube channel. It's using the following technologies:

""" disclaimer = """⚠️This is an unofficial website.\
**Intended Use**: this app for demonstration purposes; not to serve as replacement for Coach Rick official media channels or personal expertise.""" with gr.Blocks(theme=theme, analytics_enabled=False, css=css) as demo: with gr.Column(): gr.Markdown(description) gr.Markdown(disclaimer) with gr.Row(): with gr.Column(): question = gr.Textbox( placeholder="Enter your question here", lines=5, label="Question" ) submit = gr.Button("Ask", variant="primary") output = gr.Textbox(elem_id="q-output", lines=10, label="Answer") video = gr.HTML('') gr.Examples( examples=examples, inputs=[question], cache_examples=False, fn=process_example, outputs=[output, video], ) submit.click( generate, inputs=[question], outputs=[output, video], ) demo.queue(concurrency_count=16).launch(debug=True)