Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from langchain_community.document_loaders import JSONLoader
|
| 4 |
from langchain_community.vectorstores import Qdrant
|
|
|
|
|
|
|
| 5 |
from langchain_community.embeddings import HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings
|
| 6 |
from sentence_transformers.cross_encoder import CrossEncoder
|
| 7 |
from groq import Groq
|
|
@@ -12,7 +14,7 @@ client = Groq(
|
|
| 12 |
|
| 13 |
# loading data
|
| 14 |
json_path = "format_food.json"
|
| 15 |
-
|
| 16 |
|
| 17 |
def metadata_func(record: dict, metadata: dict) -> dict:
|
| 18 |
metadata["title"] = record.get("title")
|
|
@@ -37,7 +39,7 @@ loader = JSONLoader(
|
|
| 37 |
)
|
| 38 |
|
| 39 |
data = loader.load()
|
| 40 |
-
|
| 41 |
# Models
|
| 42 |
# model_name = "Snowflake/snowflake-arctic-embed-xs"
|
| 43 |
# rerank_model = CrossEncoder("mixedbread-ai/mxbai-rerank-xsmall-v1")
|
|
@@ -72,9 +74,25 @@ def format_to_markdown(response_list):
|
|
| 72 |
temp_string = "\n- ".join(response_list)
|
| 73 |
return temp_string
|
| 74 |
|
| 75 |
-
def run_query(query: str, groq: bool):
|
| 76 |
print("Running Query")
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
title_and_description = f"# Best Choice:\nA {answer[0].metadata['title']}: {answer[0].page_content}"
|
| 79 |
instructions = format_to_markdown(answer[0].metadata['instructions'])
|
| 80 |
recipe = f"# Standard Method\n## Cooking time:\n{answer[0].metadata['time']}\n\n## Recipe:\n{instructions}"
|
|
@@ -97,11 +115,12 @@ def run_query(query: str, groq: bool):
|
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("Start typing below and then click **Run** to see the output.")
|
| 99 |
inp = gr.Textbox(placeholder="What sort of meal are you after?")
|
|
|
|
| 100 |
groq_button = gr.Checkbox(value=False, label="Use Llama for a better recipe?")
|
| 101 |
title_output = gr.Markdown(label="Title and description")
|
| 102 |
instructions_output = gr.Markdown(label="Recipe")
|
| 103 |
updated_recipe = gr.Markdown(label="Updated Recipe")
|
| 104 |
btn = gr.Button("Run")
|
| 105 |
-
btn.click(fn=run_query, inputs=[inp, groq_button], outputs=[title_output, instructions_output, updated_recipe])
|
| 106 |
|
| 107 |
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
from langchain_community.document_loaders import JSONLoader
|
| 4 |
from langchain_community.vectorstores import Qdrant
|
| 5 |
+
from qdrant_client.http import models as rest
|
| 6 |
+
from qdrant_client import QdrantClient, models
|
| 7 |
from langchain_community.embeddings import HuggingFaceEmbeddings, HuggingFaceBgeEmbeddings
|
| 8 |
from sentence_transformers.cross_encoder import CrossEncoder
|
| 9 |
from groq import Groq
|
|
|
|
| 14 |
|
| 15 |
# loading data
|
| 16 |
json_path = "format_food.json"
|
| 17 |
+
json_path = "llama70b_food_dump.json"
|
| 18 |
|
| 19 |
def metadata_func(record: dict, metadata: dict) -> dict:
|
| 20 |
metadata["title"] = record.get("title")
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
data = loader.load()
|
| 42 |
+
country_list = list(set([item.metadata['cuisine'] for item in data]))
|
| 43 |
# Models
|
| 44 |
# model_name = "Snowflake/snowflake-arctic-embed-xs"
|
| 45 |
# rerank_model = CrossEncoder("mixedbread-ai/mxbai-rerank-xsmall-v1")
|
|
|
|
| 74 |
temp_string = "\n- ".join(response_list)
|
| 75 |
return temp_string
|
| 76 |
|
| 77 |
+
def run_query(query: str, groq: bool, countries: str = "None"):
|
| 78 |
print("Running Query")
|
| 79 |
+
if countries != "None":
|
| 80 |
+
countries_select = models.Filter(
|
| 81 |
+
must=[
|
| 82 |
+
models.FieldCondition(
|
| 83 |
+
key="metadata.cuisine", # Adjust key based on your data structure
|
| 84 |
+
match=models.MatchValue(value=countries),
|
| 85 |
+
)
|
| 86 |
+
]
|
| 87 |
+
)
|
| 88 |
+
else:
|
| 89 |
+
countries_select = None
|
| 90 |
+
|
| 91 |
+
answer = qdrant.similarity_search(
|
| 92 |
+
query=query,
|
| 93 |
+
k=10,
|
| 94 |
+
filter=countries_select
|
| 95 |
+
)
|
| 96 |
title_and_description = f"# Best Choice:\nA {answer[0].metadata['title']}: {answer[0].page_content}"
|
| 97 |
instructions = format_to_markdown(answer[0].metadata['instructions'])
|
| 98 |
recipe = f"# Standard Method\n## Cooking time:\n{answer[0].metadata['time']}\n\n## Recipe:\n{instructions}"
|
|
|
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
gr.Markdown("Start typing below and then click **Run** to see the output.")
|
| 117 |
inp = gr.Textbox(placeholder="What sort of meal are you after?")
|
| 118 |
+
dropdown = gr.Dropdown(['None'] + country_list, label='Filter on countries', value='None')
|
| 119 |
groq_button = gr.Checkbox(value=False, label="Use Llama for a better recipe?")
|
| 120 |
title_output = gr.Markdown(label="Title and description")
|
| 121 |
instructions_output = gr.Markdown(label="Recipe")
|
| 122 |
updated_recipe = gr.Markdown(label="Updated Recipe")
|
| 123 |
btn = gr.Button("Run")
|
| 124 |
+
btn.click(fn=run_query, inputs=[inp, groq_button, dropdown], outputs=[title_output, instructions_output, updated_recipe])
|
| 125 |
|
| 126 |
demo.launch()
|