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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -24
app.py CHANGED
@@ -1,30 +1,74 @@
1
- import os
2
- import requests
3
  import gradio as gr
 
 
4
 
5
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
6
- headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
 
 
 
7
 
8
- def generate_notes(topic):
 
 
 
9
  if not topic.strip():
10
- return "⚠️ Please enter a topic first."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- prompt = (
13
- f"Write detailed, educational notes about '{topic}'. "
14
- f"Include clear explanations, key terms, and examples. "
15
- f"Use proper paragraphs and avoid repetition."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  )
17
- payload = {"inputs": prompt, "parameters": {"max_new_tokens": 600, "temperature": 0.7}}
18
- response = requests.post(API_URL, headers=headers, json=payload)
19
- result = response.json()
20
- return result[0]["generated_text"]
21
-
22
- iface = gr.Interface(
23
- fn=generate_notes,
24
- inputs=gr.Textbox(lines=2, placeholder="Enter a topic (e.g. Photosynthesis)"),
25
- outputs=gr.Textbox(lines=15, label="Generated Notes"),
26
- title="πŸ“— Detailed Note Generator (Mistral 7B via Hugging Face API)",
27
- description="Uses the Mistral-7B model hosted on Hugging Face Inference API (secure token)."
28
- )
29
-
30
- iface.launch()
 
 
 
 
 
 
 
 
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()