Spaces:
Runtime error

studyAssistant / app.py
manojjami09's picture
Update app.py
6eab3a4 verified
Raw History Blame Contribute Delete
1.99 kB
import os
from google import genai
from google.genai import types
import gradio as gr
client = genai.Client(api_key = os.getenv("GEMINI_API_KEY"))
personalities = {
"Friendly": "You are a friendly, enthusiastic, and highly encouraging Study Assistant. Your goal is to break down complex concepts into simple, beginner-friendly explanations. Use analogies and real-world examples that beginners can relate to. Always ask a follow-up question to check understanding.",
"Academic": "You are a strictly academic, highly detailed, and professional university Professor. Use precise, formal terminology, cite key concepts and structure your response. Your goal is to break down complex concepts into simple, beginner-friendly explanations. Use analogies and real-world examples that beginners can relate to. Always ask a follow-up question to check understanding."
}
def study_assistant(question, persona):
if not question.strip():
return "Please enter a question."
try:
response = client.models.generate_content(
model="gemini-2.5-flash",
config=types.GenerateContentConfig(
system_instruction=personalities[persona],
temperature=0.5,
max_output_tokens=10000,
),
contents=question,
)
return response.text or "No response returned (possibly blocked or truncated). Try rephrasing."
except Exception as e:
return f"Error: {e}"
demo = gr.Interface(
fn = study_assistant,
inputs = [gr.Textbox(label = "Question", lines = 4, placeholder = "Ask a Question ..."),
gr.Dropdown(choices=list(personalities.keys()), value = "Friendly", label = "Personality"),
],
outputs = gr.Textbox(label = "Explanation", lines = 10),
title = "Study Assistant",
description = "Ask a question to get a simple explanation from AI along with the anologies and real world examples"
)
demo.launch(ssr_mode=False)