File size: 2,559 Bytes
55cdc0e
 
 
 
c4ae4d8
55cdc0e
 
 
 
 
 
 
 
959a39f
55cdc0e
 
 
 
 
 
 
 
 
 
 
c4ae4d8
55cdc0e
 
 
 
 
 
 
 
 
 
990e37c
 
55cdc0e
 
 
 
 
c4ae4d8
55cdc0e
 
 
 
 
c4ae4d8
55cdc0e
 
 
990e37c
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
from dotenv import load_dotenv
import time
import gradio as gr
from PIL import Image
from ChatCohere import chat_completion, summarize, card_completion
from PokemonCards import choose_random_cards
from QdrantRag import NeuralSearcher, SemanticCache, qdrant_client, encoder, image_encoder, processor, sparse_encoder
load_dotenv()


searcher = NeuralSearcher("pokemon_texts", "pokemon_images", qdrant_client, encoder, image_encoder, processor, sparse_encoder)
semantic_cache = SemanticCache(qdrant_client, encoder, "semantic_cache", 0.75)

def chat_pokemon(message: str, history):
    answer = semantic_cache.search_cache(message)
    if answer != "":
        r = ""
        for c in answer:
            r += c
            time.sleep(0.001)
            yield r
    else:
        context_search = searcher.search_text(message)
        reranked_context = searcher.reranking(message, context_search)
        context = "\n\n-----------------\n\n".join(reranked_context)
        response = chat_completion(message, "Context:\n\n"+context)
        semantic_cache.upload_to_cache(message, response)
        r = ""
        for c in response:
            r += c
            time.sleep(0.001)
            yield r
    
def what_pokemon(image_input):
    save_path = Image.fromarray(image_input)
    result = searcher.search_image(save_path)
    results = "\n".join(result)
    return "You Pokemon might be:\n" + results 

def card_package(n_cards:int=5):
    description, cards = choose_random_cards(n_cards)
    package = [f"![Card {i+1}]({cards[i]})" for i in range(len(cards))]
    cards_message = "\n\n".join(package)
    natural_lang_description = card_completion(f"Can you enthusiastically describe the cards in this package?\n\n{description}")
    return "## Your package:\n\n" + cards_message + "\n\n## Description:\n\n" + summarize(natural_lang_description)


iface1 = gr.ChatInterface(fn=chat_pokemon, title="Pokemon Chatbot", description="Ask any question about Pokemon and get an answer!")
iface2 = gr.Interface(fn=what_pokemon, title="Pokemon Image Classifier", description="Upload an image of a Pokemon and get its name!", inputs="image", outputs="text")
iface3 = gr.Interface(fn=card_package, title="Pokemon Card Package", description="Get a package of random Pokemon cards!", inputs=gr.Slider(5,10,step=1), outputs=gr.Markdown(value="Your output will be displayed here", label="Card Package"))

iface = gr.TabbedInterface([iface1, iface2, iface3], ["PokemonChat", "Identify Pokemon", "Card Package"])

iface.launch(server_name="0.0.0.0", server_port=7860)