Spaces:
Runtime error
Runtime error
File size: 4,823 Bytes
c00ccba 5b962b1 88b47df bfb4523 88b47df 0cd32cd bfb4523 0cd32cd 6cc3988 27bf09a 0cd32cd 3beb245 88b47df 6cc3988 88b47df af75af6 88b47df f809e36 88b47df 3beb245 88b47df 3beb245 88b47df 3beb245 88b47df c697f2d 91442fc 88b47df 3beb245 88b47df 3beb245 88b47df 3beb245 88b47df |
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 |
#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('tennis', 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 = [
"Tell me step by step how to find out my dominant eye when I play tennis.",
"What do we look for in a great tennis player? Write out the essential attributes.",
"Who has the best tennis serve? Explain in details.",
"Compare Novak and Nadal gamestyle in details. Who is better?",
"Who is the tennis GOAT?"
]
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 Patrick Mouratoglou</h1>
</div>
<div style="text-align: left;">
<p>This is a demo to answer some popular questions from tennis fans to Coach Patrick. The information is being extracted from his official <a href="https://www.youtube.com/@patrickmouratoglou_official" 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 Patrick 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) |