Spaces:
Sleeping
Sleeping
File size: 2,330 Bytes
b697585 a3d9ef8 014409f a3d9ef8 b697585 014409f a3d9ef8 014409f b697585 014409f b697585 014409f b697585 014409f a3d9ef8 014409f b697585 014409f b697585 014409f b697585 014409f c844eeb 014409f b697585 014409f b697585 014409f b697585 014409f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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()
|