Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,30 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def generate_notes(topic):
|
| 10 |
-
|
| 11 |
-
if not topic:
|
| 12 |
return "⚠️ Please enter a topic first."
|
| 13 |
|
| 14 |
prompt = (
|
| 15 |
-
f"
|
| 16 |
-
f"Include
|
| 17 |
-
f"
|
| 18 |
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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
|
| 35 |
outputs=gr.Textbox(lines=15, label="Generated Notes"),
|
| 36 |
-
title="
|
| 37 |
-
description="
|
| 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()
|