Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ 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()
|
|
@@ -11,7 +11,7 @@ load_dotenv()
|
|
| 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 = ""
|
|
@@ -23,8 +23,7 @@ def chat_pokemon(message: str, history):
|
|
| 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 |
-
|
| 27 |
-
response = chat_completion(final_prompt)
|
| 28 |
semantic_cache.upload_to_cache(message, response)
|
| 29 |
r = ""
|
| 30 |
for c in response:
|
|
@@ -41,14 +40,14 @@ def card_package(n_cards:int=5):
|
|
| 41 |
description, cards = choose_random_cards(n_cards)
|
| 42 |
package = [f"" for i in range(len(cards))]
|
| 43 |
cards_message = "\n\n".join(package)
|
| 44 |
-
natural_lang_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
|
| 51 |
|
| 52 |
iface = gr.TabbedInterface([iface1, iface2, iface3], ["PokemonChat", "Identify Pokemon", "Card Package"])
|
| 53 |
|
| 54 |
-
iface.launch()
|
|
|
|
| 2 |
import time
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
+
from ChatCohere import chat_completion, summarize, card_completion
|
| 6 |
from PokemonCards import choose_random_cards
|
| 7 |
from QdrantRag import NeuralSearcher, SemanticCache, qdrant_client, encoder, image_encoder, processor, sparse_encoder
|
| 8 |
load_dotenv()
|
|
|
|
| 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 = ""
|
|
|
|
| 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 |
+
response = chat_completion(message, "Context:\n\n"+context)
|
|
|
|
| 27 |
semantic_cache.upload_to_cache(message, response)
|
| 28 |
r = ""
|
| 29 |
for c in response:
|
|
|
|
| 40 |
description, cards = choose_random_cards(n_cards)
|
| 41 |
package = [f"" for i in range(len(cards))]
|
| 42 |
cards_message = "\n\n".join(package)
|
| 43 |
+
natural_lang_description = card_completion(f"Can you enthusiastically describe the cards in this package?\n\n{description}")
|
| 44 |
return "## Your package:\n\n" + cards_message + "\n\n## Description:\n\n" + summarize(natural_lang_description)
|
| 45 |
|
| 46 |
|
| 47 |
iface1 = gr.ChatInterface(fn=chat_pokemon, title="Pokemon Chatbot", description="Ask any question about Pokemon and get an answer!")
|
| 48 |
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")
|
| 49 |
+
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"))
|
| 50 |
|
| 51 |
iface = gr.TabbedInterface([iface1, iface2, iface3], ["PokemonChat", "Identify Pokemon", "Card Package"])
|
| 52 |
|
| 53 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|