File size: 5,212 Bytes
bcbb061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d803ebd
bcbb061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7531a8f
 
 
bcbb061
7531a8f
 
f95085c
 
 
 
 
bcbb061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d803ebd
bcbb061
 
d803ebd
bcbb061
 
 
 
 
 
 
 
 
 
 
d803ebd
bcbb061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#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 = '<div>'
    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'''
                <iframe width="560" height="315" src="https://www.youtube.com/embed/{source}"
                title="YouTube video player" frameborder="0" allow="accelerometer; autoplay;
                clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                '''
    return answer, embed_video_html+'</div>'

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 = """
<div style="text-align: center;">
    <h1>Ask Tennis Coach Rick Macci</h1>
</div>
<div style="text-align: left;">
    <p>This is a demo to answer some popular questions from tennis fans to Coach Rick. The information is being extracted from his official <a href="https://www.youtube.com/@RickMacci" style='color: #e6b800;'>Youtube channel</a>. It's using the following technologies:</p>
    <ul>
        <li>Google PALM</li>
        <li>Gradio</li>
        <li>hkunlp/instructor-xl</li>
        <li>HuggingFace</li>
        <li>Langchain</li>
        <li>Pinecone</li>
    </ul>
</div>
"""
disclaimer = """⚠️<b>This is an unofficial website.</b>\
 <br>**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)