mrmarvelous's picture
Update app.py
30f234f verified
Raw
History Blame Contribute Delete
1.85 kB
import gradio as gr
import google.generativeai as palm
# Configure the API key
palm.configure(api_key='AIzaSyCi0mbXfp0uEBZpK7n-YnqR9tXT0tyXSM0')
# Get the model
models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
model_name = models[0].name # Assuming the first model supports text generation
# Define the prompt template
prompt_template = """
You are an expert at solving diet issues of people. Analyze the variable p and answer
whatever they ask, considering they are Indian. First, ask if they are vegetarian or non-vegetarian
and then answer according to their needs.
User question: {user_question}
Dietary preference: {diet_preference}
"""
# Function to generate a response
def generate_response(user_question, diet_preference):
prompt = prompt_template.format(
user_question=user_question,
diet_preference=diet_preference
)
completion = palm.generate_text(
model=model_name,
prompt=prompt,
max_length=200 # Adjust as per your requirement
)
return completion['text']
# Gradio Interface
def interface(user_question, diet_preference):
response = generate_response(user_question, diet_preference)
return response
# Set up Gradio interface components
question_input = gr.Textbox(label="Enter your question:")
diet_preference_input = gr.Radio(["Vegetarian", "Non-Vegetarian"], label="Select your dietary preference:")
output = gr.Textbox(label="Response:")
# Set up the Gradio interface layout
demo = gr.Interface(
fn=interface,
inputs=[question_input, diet_preference_input],
outputs=output,
title="Diet Doubt Solver ft.Versatile.ai",
description="Enter your diet-related question and get expert advice tailored to your dietary preference."
)
# Launch the demo
if __name__ == "__main__":
demo.launch()