Talhaalvi12 commited on
Commit
dcf8e70
·
verified ·
1 Parent(s): 8e4a246

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -31
app.py CHANGED
@@ -1,46 +1,30 @@
 
 
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
 
4
- # Load TinyLlama (small but smart)
5
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
  def generate_notes(topic):
10
- topic = topic.strip()
11
- if not topic:
12
  return "⚠️ Please enter a topic first."
13
 
14
  prompt = (
15
- f"You are an expert teacher. Write detailed, well-structured study notes about '{topic}'. "
16
- f"Include an introduction, main concepts, key points, and a short summary. "
17
- f"Keep the tone educational and clear for students."
18
  )
19
-
20
- inputs = tokenizer(prompt, return_tensors="pt")
21
- outputs = model.generate(
22
- **inputs,
23
- max_new_tokens=600,
24
- temperature=0.7,
25
- top_p=0.9,
26
- repetition_penalty=1.2,
27
- )
28
-
29
- text = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
- return text.replace(prompt, "").strip()
31
 
32
  iface = gr.Interface(
33
  fn=generate_notes,
34
- inputs=gr.Textbox(lines=2, placeholder="Enter a topic (e.g. Photosynthesis, World Geography)"),
35
  outputs=gr.Textbox(lines=15, label="Generated Notes"),
36
- title="📚 AI Note Generator (TinyLlama 1.1B Chat)",
37
- description="Generate long, educational notes for free using the TinyLlama 1.1B Chat model.",
38
- examples=[
39
- ["Photosynthesis process"],
40
- ["Causes of World War II"],
41
- ["Artificial Intelligence"],
42
- ["The Water Cycle"]
43
- ]
44
  )
45
 
46
  iface.launch()
 
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()