Talhaalvi12 commited on
Commit
b697585
Β·
verified Β·
1 Parent(s): 014409f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -1,21 +1,22 @@
 
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",
@@ -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 note-taking assistant."},
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
- # 🎨 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()
 
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()