Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,74 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
if not topic.strip():
|
| 10 |
-
return "β οΈ Please enter a topic
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
+
# ======================
|
| 6 |
+
# π Your API Key
|
| 7 |
+
# ======================
|
| 8 |
+
API_KEY = "sk-9bUOTBxrvS2p5e8z7kcyZEC7ocsw84DonYz3wWt1m94vC9PA"
|
| 9 |
+
BASE_URL = "https://api.chatanywhere.tech/v1/chat/completions"
|
| 10 |
|
| 11 |
+
# ======================
|
| 12 |
+
# π¬ Function to call ChatAnywhere API
|
| 13 |
+
# ======================
|
| 14 |
+
def generate_notes(topic, model_choice):
|
| 15 |
if not topic.strip():
|
| 16 |
+
return "β οΈ Please enter a topic."
|
| 17 |
+
|
| 18 |
+
prompt = f"Create well-structured, easy-to-understand study notes about {topic}."
|
| 19 |
+
|
| 20 |
+
headers = {
|
| 21 |
+
"Content-Type": "application/json",
|
| 22 |
+
"Authorization": f"Bearer {API_KEY}"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
payload = {
|
| 26 |
+
"model": model_choice,
|
| 27 |
+
"messages": [
|
| 28 |
+
{"role": "system", "content": "You are a helpful note-taking assistant."},
|
| 29 |
+
{"role": "user", "content": prompt}
|
| 30 |
+
],
|
| 31 |
+
"temperature": 0.7
|
| 32 |
+
}
|
| 33 |
|
| 34 |
+
try:
|
| 35 |
+
response = requests.post(BASE_URL, headers=headers, json=payload)
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
result = response.json()
|
| 38 |
+
notes = result["choices"][0]["message"]["content"]
|
| 39 |
+
return notes.strip()
|
| 40 |
+
else:
|
| 41 |
+
return f"β Error {response.status_code}: {response.text}"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"β οΈ Exception: {e}"
|
| 44 |
+
|
| 45 |
+
# ======================
|
| 46 |
+
# π¨ Gradio UI
|
| 47 |
+
# ======================
|
| 48 |
+
with gr.Blocks(title="π AI Study Notes Generator") as demo:
|
| 49 |
+
gr.Markdown(
|
| 50 |
+
"""
|
| 51 |
+
# π AI Study Notes Generator
|
| 52 |
+
Type a topic, choose a model, and generate easy-to-understand notes.
|
| 53 |
+
Powered by **ChatAnywhere API**.
|
| 54 |
+
"""
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
topic_input = gr.Textbox(label="Enter a Topic", placeholder="e.g. Photosynthesis, Machine Learning...")
|
| 59 |
+
model_choice = gr.Dropdown(
|
| 60 |
+
["gpt-4o-mini", "gpt-4o", "gpt-3.5-turbo", "deepseek-chat", "gpt-5"],
|
| 61 |
+
value="gpt-4o-mini",
|
| 62 |
+
label="Select Model"
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
generate_button = gr.Button("β¨ Generate Notes")
|
| 66 |
+
output_box = gr.Textbox(label="Generated Notes", lines=20)
|
| 67 |
+
|
| 68 |
+
generate_button.click(fn=generate_notes, inputs=[topic_input, model_choice], outputs=output_box)
|
| 69 |
+
|
| 70 |
+
# ======================
|
| 71 |
+
# π Launch App
|
| 72 |
+
# ======================
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|