Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 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 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
headers = {
|
| 21 |
"Content-Type": "application/json",
|
|
@@ -25,7 +26,7 @@ def generate_notes(topic, model_choice):
|
|
| 25 |
payload = {
|
| 26 |
"model": model_choice,
|
| 27 |
"messages": [
|
| 28 |
-
{"role": "system", "content": "You are a helpful
|
| 29 |
{"role": "user", "content": prompt}
|
| 30 |
],
|
| 31 |
"temperature": 0.7
|
|
@@ -40,35 +41,30 @@ def generate_notes(topic, model_choice):
|
|
| 40 |
else:
|
| 41 |
return f"β Error {response.status_code}: {response.text}"
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"β οΈ Exception: {e}"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
# ======================
|
| 48 |
-
with gr.Blocks(title="π AI Study Notes Generator") as demo:
|
| 49 |
gr.Markdown(
|
| 50 |
"""
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
Powered by
|
| 54 |
"""
|
| 55 |
)
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
-
|
| 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 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
|
| 70 |
-
# ======================
|
| 71 |
-
# π Launch App
|
| 72 |
-
# ======================
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
# β
Get API key from Hugging Face Space secrets
|
| 7 |
+
API_KEY = os.getenv("sk-9bUOTBxrvS2p5e8z7kcyZEC7ocsw84DonYz3wWt1m94vC9PA")
|
|
|
|
|
|
|
| 8 |
BASE_URL = "https://api.chatanywhere.tech/v1/chat/completions"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def generate_notes(topic, model_choice):
|
| 11 |
+
topic = topic.strip()
|
| 12 |
+
if not topic:
|
| 13 |
+
return "β οΈ Please enter a topic first."
|
| 14 |
|
| 15 |
+
prompt = (
|
| 16 |
+
f"You are an expert teacher. Write detailed, well-structured study notes about '{topic}'. "
|
| 17 |
+
f"Include an introduction, main concepts, key points, and a short summary. "
|
| 18 |
+
f"Keep the tone educational, clear, and easy for students to understand."
|
| 19 |
+
)
|
| 20 |
|
| 21 |
headers = {
|
| 22 |
"Content-Type": "application/json",
|
|
|
|
| 26 |
payload = {
|
| 27 |
"model": model_choice,
|
| 28 |
"messages": [
|
| 29 |
+
{"role": "system", "content": "You are a helpful and knowledgeable teacher."},
|
| 30 |
{"role": "user", "content": prompt}
|
| 31 |
],
|
| 32 |
"temperature": 0.7
|
|
|
|
| 41 |
else:
|
| 42 |
return f"β Error {response.status_code}: {response.text}"
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"β οΈ Exception: {str(e)}"
|
| 45 |
|
| 46 |
+
# π¨ Build Gradio interface
|
| 47 |
+
with gr.Blocks(title="π ChatAnywhere AI Notes Generator") as demo:
|
|
|
|
|
|
|
| 48 |
gr.Markdown(
|
| 49 |
"""
|
| 50 |
+
# π ChatAnywhere Study Notes Generator
|
| 51 |
+
Enter a topic below and generate detailed, structured notes.
|
| 52 |
+
π‘ Powered by ChatAnywhere API.
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
+
topic = gr.Textbox(label="Enter a Topic", placeholder="e.g. Photosynthesis, AI, World War II")
|
| 58 |
model_choice = gr.Dropdown(
|
| 59 |
["gpt-4o-mini", "gpt-4o", "gpt-3.5-turbo", "deepseek-chat", "gpt-5"],
|
| 60 |
value="gpt-4o-mini",
|
| 61 |
label="Select Model"
|
| 62 |
)
|
| 63 |
|
| 64 |
+
generate_btn = gr.Button("β¨ Generate Notes")
|
| 65 |
+
output = gr.Textbox(label="Generated Notes", lines=20)
|
| 66 |
|
| 67 |
+
generate_btn.click(fn=generate_notes, inputs=[topic, model_choice], outputs=output)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|