Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +59 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain_core.output_parsers import JsonOutputParser
|
| 3 |
+
from langchain_core.prompts import PromptTemplate
|
| 4 |
+
from langchain_ollama import ChatOllama
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Initialize the model and parser
|
| 8 |
+
model = ChatOllama(
|
| 9 |
+
model="llama3.2",
|
| 10 |
+
temperature=0.25,
|
| 11 |
+
base_url="https://alpaca-upright-vulture.ngrok-free.app"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
parser = JsonOutputParser()
|
| 15 |
+
prompt = PromptTemplate(
|
| 16 |
+
template="Answer the user query.\n{format_instructions}\n{query}\n",
|
| 17 |
+
input_variables=["query"],
|
| 18 |
+
partial_variables={"format_instructions": parser.get_format_instructions()}
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
chain = prompt | model | parser
|
| 22 |
+
|
| 23 |
+
def generate_recipe_json(query):
|
| 24 |
+
try:
|
| 25 |
+
results = chain.invoke({"query": query})
|
| 26 |
+
# Format JSON with indentation for better readability
|
| 27 |
+
formatted_json = json.dumps(results, indent=2)
|
| 28 |
+
return formatted_json
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error: {str(e)}"
|
| 31 |
+
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
with gr.Blocks(title="Recipe JSON Generator") as demo:
|
| 34 |
+
gr.Markdown("# 🍳 Recipe JSON Generator")
|
| 35 |
+
gr.Markdown("Get recipe information in JSON format!")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
query_input = gr.Textbox(
|
| 39 |
+
label="Your Query",
|
| 40 |
+
placeholder="Enter your recipe query...",
|
| 41 |
+
value="how to make gulab jam with ingredients"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
generate_btn = gr.Button("Generate Recipe JSON")
|
| 45 |
+
|
| 46 |
+
json_output = gr.Code(
|
| 47 |
+
label="Recipe JSON",
|
| 48 |
+
language="json",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
generate_btn.click(
|
| 52 |
+
fn=generate_recipe_json,
|
| 53 |
+
inputs=[query_input],
|
| 54 |
+
outputs=[json_output]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Launch the interface
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain-ollama
|
| 2 |
+
langchain-core
|
| 3 |
+
gradio
|