DurgaDeepak commited on
Commit
e05063c
Β·
verified Β·
1 Parent(s): 30f2776

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -50
app.py CHANGED
@@ -1,66 +1,70 @@
1
  import gradio as gr
2
  import spaces
3
- from huggingface_hub import InferenceClient
4
 
5
- """
6
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
7
- """
8
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
 
 
 
 
 
 
 
 
 
 
 
 
10
  @spaces.GPU
11
  def respond(
12
- message,
13
  history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
 
18
  ):
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
-
27
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- response = ""
 
 
30
 
31
- for message in client.chat_completion(
32
- messages,
33
- max_tokens=max_tokens,
34
- stream=True,
35
- temperature=temperature,
36
- top_p=top_p,
37
- ):
38
- token = message.choices[0].delta.content
39
-
40
- response += token
41
- yield response
42
-
43
-
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
47
 
48
  demo = gr.ChatInterface(
49
- respond,
50
- additional_inputs=[
51
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
52
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
53
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
54
- gr.Slider(
55
- minimum=0.1,
56
- maximum=1.0,
57
- value=0.95,
58
- step=0.05,
59
- label="Top-p (nucleus sampling)",
60
- ),
61
- ],
62
  )
63
 
64
-
65
  if __name__ == "__main__":
66
  demo.launch()
 
1
  import gradio as gr
2
  import spaces
3
+ from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
4
 
5
+ # β€”β€”β€” Configuration β€”β€”β€”
6
+ MODEL_NAME = "facebook/rag-sequence-nq"
7
+ DATASET_NAME = "username/mealplan-chunks"
 
8
 
9
+ # β€”β€”β€” Initialize RAG β€”β€”β€”
10
+ tokenizer = RagTokenizer.from_pretrained(MODEL_NAME)
11
+ retriever = RagRetriever.from_pretrained(
12
+ MODEL_NAME,
13
+ index_name="exact",
14
+ use_dummy_dataset=False,
15
+ dataset_name=DATASET_NAME
16
+ )
17
+ model = RagSequenceForGeneration.from_pretrained(MODEL_NAME, retriever=retriever)
18
+
19
+ # β€”β€”β€” Core chat callback β€”β€”β€”
20
  @spaces.GPU
21
  def respond(
22
+ message: str,
23
  history: list[tuple[str, str]],
24
+ goal: str,
25
+ diet: list[str],
26
+ meals: int,
27
+ avoid: str,
28
+ weeks: str,
29
  ):
30
+ # parse avoidances
31
+ avoid_list = [a.strip() for a in avoid.split(",") if a.strip()]
32
+ # build prefs string
33
+ prefs = (
34
+ f"Goal={goal}; "
35
+ f"Diet={','.join(diet)}; "
36
+ f"Meals={meals}/day; "
37
+ f"Avoid={','.join(avoid_list)}; "
38
+ f"Duration={weeks}"
39
+ )
40
+ # system guardrail + prefs + question
41
+ prompt = (
42
+ "SYSTEM: Only answer using the provided CONTEXT. "
43
+ "If it’s not there, say β€œI’m sorry, I don’t know.”\n"
44
+ f"PREFS: {prefs}\n"
45
+ f"Q: {message}\n"
46
+ )
47
+ # generate
48
+ inputs = tokenizer([prompt], return_tensors="pt")
49
+ outputs = model.generate(**inputs, num_beams=2, max_new_tokens=200)
50
+ answer = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
51
 
52
+ history = history or []
53
+ history.append((message, answer))
54
+ return history
55
 
56
+ # β€”β€”β€” Build the Chat Interface β€”β€”β€”
57
+ # preference controls
58
+ goal = gr.Dropdown(["Lose weight","Bulk","Maintain"], value="Lose weight", label="Goal")
59
+ diet = gr.CheckboxGroup(["Omnivore","Vegetarian","Vegan","Keto","Paleo","Low-Carb"], label="Diet Style")
60
+ meals = gr.Slider(1, 6, value=3, step=1, label="Meals per day")
61
+ avoid = gr.Textbox(placeholder="e.g. Gluten, Dairy, Nuts, Eggs, Soy…", label="Avoidances (comma-separated)")
62
+ weeks = gr.Dropdown(["1 week","2 weeks","3 weeks","4 weeks"], value="1 week", label="Plan Length")
 
 
 
 
 
 
 
 
 
63
 
64
  demo = gr.ChatInterface(
65
+ fn=respond,
66
+ additional_inputs=[goal, diet, meals, avoid, weeks],
 
 
 
 
 
 
 
 
 
 
 
67
  )
68
 
 
69
  if __name__ == "__main__":
70
  demo.launch()