Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from groq import Groq | |
| client = Groq( | |
| api_key=os.environ.get("GROQ_API_KEY"), | |
| ) | |
| def shop_response(message, history): | |
| shop_data = { | |
| "name": "Green Lane Grocers", | |
| "address": "123 Market Lane", | |
| "hours": "Mon–Sat 9:00–19:00", | |
| "products": [ | |
| {"sku": "vanilla_cookies", "name": "Vanilla Cookies", "price": "₹120", "availability": "12 packs in stock"}, | |
| {"sku": "brown_bread", "name": "Brown Bread", "price": "₹50", "availability": "Out of stock"}, | |
| ] | |
| } | |
| context_prompt = f""" | |
| You are ShopAssist, the friendly assistant for {shop_data['name']}. | |
| Always answer based on this shop's details: | |
| - Address: {shop_data['address']} | |
| - Hours: {shop_data['hours']} | |
| - Products: {shop_data['products']} | |
| If the user asks something not in this info, say "I’m not sure — let me connect you to the shop." | |
| Keep answers polite, short, and clear. | |
| """ | |
| messages=[{"role":"system","content":context_prompt}] | |
| #adding past conversations | |
| trimmed_history=history[-4:] | |
| for turn in history: | |
| messages.append({"role":turn["role"], "content": turn["content"]}) | |
| #adding current user query | |
| messages.append({"role":"user","content":message}) | |
| #Calling Groq API | |
| chat_completion = client.chat.completions.create( | |
| messages=messages, | |
| model="llama-3.3-70b-versatile", | |
| temperature=0.2 | |
| ) | |
| return chat_completion.choices[0].message.content | |
| demo = gr.ChatInterface(fn=shop_response, type="messages",title="Shop Assistant - Green Lane Grocers",description="Ask me about shop hours, location, or products!") | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |