NotesGenerator / app.py
Talhaalvi12's picture
Update app.py
b697585 verified
import os
import gradio as gr
import requests
import json
# βœ… Get API key from Hugging Face Space secrets
API_KEY = os.getenv("sk-9bUOTBxrvS2p5e8z7kcyZEC7ocsw84DonYz3wWt1m94vC9PA")
BASE_URL = "https://api.chatanywhere.tech/v1/chat/completions"
def generate_notes(topic, model_choice):
topic = topic.strip()
if not topic:
return "⚠️ Please enter a topic first."
prompt = (
f"You are an expert teacher. Write detailed, well-structured study notes about '{topic}'. "
f"Include an introduction, main concepts, key points, and a short summary. "
f"Keep the tone educational, clear, and easy for students to understand."
)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
payload = {
"model": model_choice,
"messages": [
{"role": "system", "content": "You are a helpful and knowledgeable teacher."},
{"role": "user", "content": prompt}
],
"temperature": 0.7
}
try:
response = requests.post(BASE_URL, headers=headers, json=payload)
if response.status_code == 200:
result = response.json()
notes = result["choices"][0]["message"]["content"]
return notes.strip()
else:
return f"❌ Error {response.status_code}: {response.text}"
except Exception as e:
return f"⚠️ Exception: {str(e)}"
# 🎨 Build Gradio interface
with gr.Blocks(title="πŸ“˜ ChatAnywhere AI Notes Generator") as demo:
gr.Markdown(
"""
# πŸ“š ChatAnywhere Study Notes Generator
Enter a topic below and generate detailed, structured notes.
πŸ’‘ Powered by ChatAnywhere API.
"""
)
with gr.Row():
topic = gr.Textbox(label="Enter a Topic", placeholder="e.g. Photosynthesis, AI, World War II")
model_choice = gr.Dropdown(
["gpt-4o-mini", "gpt-4o", "gpt-3.5-turbo", "deepseek-chat", "gpt-5"],
value="gpt-4o-mini",
label="Select Model"
)
generate_btn = gr.Button("✨ Generate Notes")
output = gr.Textbox(label="Generated Notes", lines=20)
generate_btn.click(fn=generate_notes, inputs=[topic, model_choice], outputs=output)
if __name__ == "__main__":
demo.launch()