3v324v23 commited on
Commit
890ed83
·
1 Parent(s): 4523f25

Back to InferenceClient with Mixtral-8x7B

Browse files
Files changed (2) hide show
  1. app.py +14 -13
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,35 +1,36 @@
1
  import os
2
- import requests
3
  import streamlit as st
 
4
 
5
  st.set_page_config(page_title="Recipe Generator", layout="centered")
6
 
7
  st.title("🍳 AI Recipe Generator")
8
  st.write("Enter ingredients and get a recipe suggestion!")
9
 
10
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/v1/chat/completions"
 
 
 
 
 
11
 
12
  ingredients = st.text_input("Enter ingredients (comma-separated):", placeholder="eggs, cheese, spinach")
13
 
14
  if st.button("Generate Recipe"):
15
  if ingredients:
16
  with st.spinner("Generating recipe..."):
17
- token = os.environ.get("HF_TOKEN")
18
- headers = {"Authorization": f"Bearer {token}"}
19
  prompt = (
20
  f"Write a complete recipe using these ingredients: {ingredients}. "
21
  "Include: a creative recipe name, ingredients with amounts, "
22
  "numbered cooking steps, and estimated cooking time."
23
  )
24
- payload = {
25
- "model": "HuggingFaceH4/zephyr-7b-beta",
26
- "messages": [{"role": "user", "content": prompt}],
27
- "max_tokens": 500,
28
- "temperature": 0.7,
29
- }
30
- response = requests.post(API_URL, headers=headers, json=payload)
31
- response.raise_for_status()
32
- recipe = response.json()["choices"][0]["message"]["content"]
33
  st.success(recipe)
34
  else:
35
  st.warning("Please enter some ingredients!")
 
1
  import os
 
2
  import streamlit as st
3
+ from huggingface_hub import InferenceClient
4
 
5
  st.set_page_config(page_title="Recipe Generator", layout="centered")
6
 
7
  st.title("🍳 AI Recipe Generator")
8
  st.write("Enter ingredients and get a recipe suggestion!")
9
 
10
+ @st.cache_resource
11
+ def get_client():
12
+ token = os.environ.get("HF_TOKEN")
13
+ return InferenceClient(token=token)
14
+
15
+ client = get_client()
16
 
17
  ingredients = st.text_input("Enter ingredients (comma-separated):", placeholder="eggs, cheese, spinach")
18
 
19
  if st.button("Generate Recipe"):
20
  if ingredients:
21
  with st.spinner("Generating recipe..."):
 
 
22
  prompt = (
23
  f"Write a complete recipe using these ingredients: {ingredients}. "
24
  "Include: a creative recipe name, ingredients with amounts, "
25
  "numbered cooking steps, and estimated cooking time."
26
  )
27
+ response = client.chat_completion(
28
+ model="mistralai/Mixtral-8x7B-Instruct-v0.1",
29
+ messages=[{"role": "user", "content": prompt}],
30
+ max_tokens=500,
31
+ temperature=0.7,
32
+ )
33
+ recipe = response.choices[0].message.content
 
 
34
  st.success(recipe)
35
  else:
36
  st.warning("Please enter some ingredients!")
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
  streamlit>=1.28.0
2
- requests>=2.31.0
 
1
  streamlit>=1.28.0
2
+ huggingface_hub>=0.23.0