as-cle-bert commited on
Commit
55cdc0e
·
verified ·
1 Parent(s): 00b0db9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import time
3
+ import gradio as gr
4
+ from PIL import Image
5
+ from ChatCohere import chat_completion, summarize
6
+ from PokemonCards import choose_random_cards
7
+ from QdrantRag import NeuralSearcher, SemanticCache, qdrant_client, encoder, image_encoder, processor, sparse_encoder
8
+ load_dotenv()
9
+
10
+
11
+ searcher = NeuralSearcher("pokemon_texts", "pokemon_images", qdrant_client, encoder, image_encoder, processor, sparse_encoder)
12
+ semantic_cache = SemanticCache(qdrant_client, encoder, "semantic_cache", 0.75)
13
+
14
+ def chat_pokemon(message: str):
15
+ answer = semantic_cache.search_cache(message)
16
+ if answer != "":
17
+ r = ""
18
+ for c in answer:
19
+ r += c
20
+ time.sleep(0.001)
21
+ yield r
22
+ else:
23
+ context_search = searcher.search_text(message)
24
+ reranked_context = searcher.reranking(message, context_search)
25
+ context = "\n\n-----------------\n\n".join(reranked_context)
26
+ final_prompt = f"USER QUERY:\n\n{message.content}\n\nCONTEXT:\n\n{context}"
27
+ response = chat_completion(final_prompt)
28
+ semantic_cache.upload_to_cache(message, response)
29
+ r = ""
30
+ for c in response:
31
+ r += c
32
+ time.sleep(0.001)
33
+ yield r
34
+
35
+ def what_pokemon(image_input):
36
+ save_path = Image.fromarray(image_input)
37
+ result = searcher.search_image(save_path)
38
+ return "You Pokemon might be: " + result[0]
39
+
40
+ def card_package(n_cards:int=5):
41
+ description, cards = choose_random_cards(n_cards)
42
+ package = [f"![Card {i+1}]({cards[i]})" for i in range(len(cards))]
43
+ cards_message = "\n\n".join(package)
44
+ natural_lang_description = chat_completion([{"role": "system", "content": "You are an expert in Pokemon cards. You know everything about their characteristics, abilities, and evolutions. You always reply with the most accurate information, you are polite and helpful."}, {"role": "user", "content": f"Can you enthusiastically describe the cards in this package?\n\n{description}"}])
45
+ return "## Your package:\n\n" + cards_message + "\n\n## Description:\n\n" + summarize(natural_lang_description)
46
+
47
+
48
+ iface1 = gr.ChatInterface(fn=chat_pokemon, title="Pokemon Chatbot", description="Ask any question about Pokemon and get an answer!")
49
+ 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")
50
+ 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"))
51
+
52
+ iface = gr.TabbedInterface([iface1, iface2, iface3], ["PokemonChat", "Identify Pokemon", "Card Package"])
53
+
54
+ iface.launch()