Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,20 +12,24 @@ def resolve_token(ui_token):
|
|
| 12 |
if ui_token and ui_token.strip():
|
| 13 |
return ui_token.strip()
|
| 14 |
|
| 15 |
-
env_token = os.getenv("
|
| 16 |
if env_token:
|
| 17 |
return env_token.strip()
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
SUPPORTED_EXT = (
|
| 20 |
".pdf", ".docx", ".txt", ".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff"
|
| 21 |
)
|
| 22 |
|
|
|
|
| 23 |
def extract_text_from_file(filepath):
|
| 24 |
if not filepath:
|
| 25 |
return ""
|
| 26 |
|
| 27 |
-
if
|
| 28 |
-
filepath = filepath
|
| 29 |
|
| 30 |
ext = pathlib.Path(filepath).suffix.lower()
|
| 31 |
|
|
@@ -52,20 +56,18 @@ def extract_text_from_file(filepath):
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error reading file: {str(e)}"
|
| 54 |
|
|
|
|
| 55 |
MODELS = {
|
| 56 |
"Qwen 3.5 0.8B (Fastest)": "Qwen/Qwen3.5-0.8B",
|
| 57 |
"DeepSeek R1 1.5B": "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
|
| 58 |
"Qwen 3.5 2B (Balanced Speed)": "Qwen/Qwen3.5-2B",
|
| 59 |
-
|
| 60 |
"Gemma 4 5B": "google/gemma-4-E2B-it",
|
| 61 |
"Qwen 3.5 4B": "Qwen/Qwen3.5-4B",
|
| 62 |
"Gemma 4 9B": "google/gemma-4-9b-it",
|
| 63 |
-
|
| 64 |
"DeepSeek Qwen 7B": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
|
| 65 |
"DeepSeek R1 Llama 8B": "deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
|
| 66 |
"Gemma 8B": "google/gemma-4-E4B-it",
|
| 67 |
"Qwen 3.5 9B (Balanced Thinking)": "Qwen/Qwen3.5-9B",
|
| 68 |
-
|
| 69 |
"Qwen 3.6 27B (Best)": "Qwen/Qwen3.6-27B",
|
| 70 |
}
|
| 71 |
|
|
@@ -76,15 +78,14 @@ Follow instructions strictly.
|
|
| 76 |
Use simple language.
|
| 77 |
Be structured.
|
| 78 |
Avoid repetition.
|
| 79 |
-
Output
|
| 80 |
"""
|
| 81 |
|
| 82 |
-
def make_prompts(topic):
|
| 83 |
|
|
|
|
| 84 |
base = f"""
|
| 85 |
Topic:
|
| 86 |
{topic}
|
| 87 |
-
|
| 88 |
Instructions:
|
| 89 |
- Be clear and concise
|
| 90 |
- Use bullet points
|
|
@@ -92,10 +93,8 @@ Instructions:
|
|
| 92 |
"""
|
| 93 |
|
| 94 |
return {
|
| 95 |
-
|
| 96 |
"lesson": base + """
|
| 97 |
Create a lesson plan with:
|
| 98 |
-
|
| 99 |
1. Objectives (3-5 points)
|
| 100 |
2. Short introduction
|
| 101 |
3. Key concepts (bullet points)
|
|
@@ -104,48 +103,47 @@ Create a lesson plan with:
|
|
| 104 |
6. 1 classroom activity
|
| 105 |
7. 5 assessment questions
|
| 106 |
""",
|
| 107 |
-
|
| 108 |
"qa": base + """
|
| 109 |
Generate 10 short exam questions with answers.
|
| 110 |
Keep answers brief (2-3 lines each).
|
| 111 |
""",
|
| 112 |
-
|
| 113 |
"mcq": base + """
|
| 114 |
Generate 10 MCQs:
|
| 115 |
- 4 options each
|
| 116 |
- mark correct answer
|
| 117 |
""",
|
| 118 |
-
|
| 119 |
"summary": base + """
|
| 120 |
Write a 200-word summary.
|
| 121 |
Use simple sentences.
|
| 122 |
"""
|
| 123 |
}
|
| 124 |
|
| 125 |
-
def stream_llm(model_id, prompt, hf_token):
|
| 126 |
|
|
|
|
| 127 |
if not hf_token:
|
| 128 |
yield "β No Hugging Face API key found."
|
| 129 |
return
|
| 130 |
|
| 131 |
try:
|
| 132 |
-
client = InferenceClient(
|
| 133 |
-
model=model_id,
|
| 134 |
-
token=hf_token,
|
| 135 |
-
)
|
| 136 |
|
| 137 |
stream = client.text_generation(
|
| 138 |
SYSTEM_MSG + "\n\n" + prompt,
|
| 139 |
max_new_tokens=1024,
|
| 140 |
-
temperature=0.
|
| 141 |
-
top_p=0.
|
| 142 |
repetition_penalty=1.1,
|
| 143 |
stream=True,
|
| 144 |
)
|
| 145 |
|
| 146 |
partial = ""
|
| 147 |
-
for
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
yield partial
|
| 150 |
|
| 151 |
except Exception as e:
|
|
@@ -155,8 +153,8 @@ def stream_llm(model_id, prompt, hf_token):
|
|
| 155 |
else:
|
| 156 |
yield f"β API Error:\n{err}"
|
| 157 |
|
| 158 |
-
def generate_content(text, file, model_label, token):
|
| 159 |
|
|
|
|
| 160 |
file_text = extract_text_from_file(file) if file else ""
|
| 161 |
syllabus = (text + "\n\n" + file_text).strip()
|
| 162 |
|
|
@@ -175,6 +173,7 @@ def generate_content(text, file, model_label, token):
|
|
| 175 |
outputs[i] = chunk
|
| 176 |
yield tuple(outputs)
|
| 177 |
|
|
|
|
| 178 |
CSS = """
|
| 179 |
body,.gradio-container{
|
| 180 |
font-family: Inter, sans-serif !important;
|
|
@@ -185,7 +184,6 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 185 |
gr.Markdown("# π AI Study Material Generator (Streaming)")
|
| 186 |
|
| 187 |
with gr.Row():
|
| 188 |
-
|
| 189 |
with gr.Column():
|
| 190 |
text_input = gr.Textbox(
|
| 191 |
placeholder="Paste syllabus or topic",
|
|
@@ -208,7 +206,6 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 208 |
btn = gr.Button("Generate")
|
| 209 |
|
| 210 |
with gr.Tabs():
|
| 211 |
-
|
| 212 |
with gr.TabItem("Lesson Plan"):
|
| 213 |
lesson = gr.Markdown()
|
| 214 |
|
|
|
|
| 12 |
if ui_token and ui_token.strip():
|
| 13 |
return ui_token.strip()
|
| 14 |
|
| 15 |
+
env_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 16 |
if env_token:
|
| 17 |
return env_token.strip()
|
| 18 |
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
|
| 22 |
SUPPORTED_EXT = (
|
| 23 |
".pdf", ".docx", ".txt", ".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff"
|
| 24 |
)
|
| 25 |
|
| 26 |
+
|
| 27 |
def extract_text_from_file(filepath):
|
| 28 |
if not filepath:
|
| 29 |
return ""
|
| 30 |
|
| 31 |
+
if isinstance(filepath, dict) and "name" in filepath:
|
| 32 |
+
filepath = filepath["name"]
|
| 33 |
|
| 34 |
ext = pathlib.Path(filepath).suffix.lower()
|
| 35 |
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error reading file: {str(e)}"
|
| 58 |
|
| 59 |
+
|
| 60 |
MODELS = {
|
| 61 |
"Qwen 3.5 0.8B (Fastest)": "Qwen/Qwen3.5-0.8B",
|
| 62 |
"DeepSeek R1 1.5B": "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
|
| 63 |
"Qwen 3.5 2B (Balanced Speed)": "Qwen/Qwen3.5-2B",
|
|
|
|
| 64 |
"Gemma 4 5B": "google/gemma-4-E2B-it",
|
| 65 |
"Qwen 3.5 4B": "Qwen/Qwen3.5-4B",
|
| 66 |
"Gemma 4 9B": "google/gemma-4-9b-it",
|
|
|
|
| 67 |
"DeepSeek Qwen 7B": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
|
| 68 |
"DeepSeek R1 Llama 8B": "deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
|
| 69 |
"Gemma 8B": "google/gemma-4-E4B-it",
|
| 70 |
"Qwen 3.5 9B (Balanced Thinking)": "Qwen/Qwen3.5-9B",
|
|
|
|
| 71 |
"Qwen 3.6 27B (Best)": "Qwen/Qwen3.6-27B",
|
| 72 |
}
|
| 73 |
|
|
|
|
| 78 |
Use simple language.
|
| 79 |
Be structured.
|
| 80 |
Avoid repetition.
|
| 81 |
+
Output markdown.
|
| 82 |
"""
|
| 83 |
|
|
|
|
| 84 |
|
| 85 |
+
def make_prompts(topic):
|
| 86 |
base = f"""
|
| 87 |
Topic:
|
| 88 |
{topic}
|
|
|
|
| 89 |
Instructions:
|
| 90 |
- Be clear and concise
|
| 91 |
- Use bullet points
|
|
|
|
| 93 |
"""
|
| 94 |
|
| 95 |
return {
|
|
|
|
| 96 |
"lesson": base + """
|
| 97 |
Create a lesson plan with:
|
|
|
|
| 98 |
1. Objectives (3-5 points)
|
| 99 |
2. Short introduction
|
| 100 |
3. Key concepts (bullet points)
|
|
|
|
| 103 |
6. 1 classroom activity
|
| 104 |
7. 5 assessment questions
|
| 105 |
""",
|
|
|
|
| 106 |
"qa": base + """
|
| 107 |
Generate 10 short exam questions with answers.
|
| 108 |
Keep answers brief (2-3 lines each).
|
| 109 |
""",
|
|
|
|
| 110 |
"mcq": base + """
|
| 111 |
Generate 10 MCQs:
|
| 112 |
- 4 options each
|
| 113 |
- mark correct answer
|
| 114 |
""",
|
|
|
|
| 115 |
"summary": base + """
|
| 116 |
Write a 200-word summary.
|
| 117 |
Use simple sentences.
|
| 118 |
"""
|
| 119 |
}
|
| 120 |
|
|
|
|
| 121 |
|
| 122 |
+
def stream_llm(model_id, prompt, hf_token):
|
| 123 |
if not hf_token:
|
| 124 |
yield "β No Hugging Face API key found."
|
| 125 |
return
|
| 126 |
|
| 127 |
try:
|
| 128 |
+
client = InferenceClient(model=model_id, token=hf_token)
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
stream = client.text_generation(
|
| 131 |
SYSTEM_MSG + "\n\n" + prompt,
|
| 132 |
max_new_tokens=1024,
|
| 133 |
+
temperature=0.7,
|
| 134 |
+
top_p=0.95,
|
| 135 |
repetition_penalty=1.1,
|
| 136 |
stream=True,
|
| 137 |
)
|
| 138 |
|
| 139 |
partial = ""
|
| 140 |
+
for chunk in stream:
|
| 141 |
+
if hasattr(chunk, "token"):
|
| 142 |
+
token_text = chunk.token.text
|
| 143 |
+
else:
|
| 144 |
+
token_text = str(chunk)
|
| 145 |
+
|
| 146 |
+
partial += token_text
|
| 147 |
yield partial
|
| 148 |
|
| 149 |
except Exception as e:
|
|
|
|
| 153 |
else:
|
| 154 |
yield f"β API Error:\n{err}"
|
| 155 |
|
|
|
|
| 156 |
|
| 157 |
+
def generate_content(text, file, model_label, token):
|
| 158 |
file_text = extract_text_from_file(file) if file else ""
|
| 159 |
syllabus = (text + "\n\n" + file_text).strip()
|
| 160 |
|
|
|
|
| 173 |
outputs[i] = chunk
|
| 174 |
yield tuple(outputs)
|
| 175 |
|
| 176 |
+
|
| 177 |
CSS = """
|
| 178 |
body,.gradio-container{
|
| 179 |
font-family: Inter, sans-serif !important;
|
|
|
|
| 184 |
gr.Markdown("# π AI Study Material Generator (Streaming)")
|
| 185 |
|
| 186 |
with gr.Row():
|
|
|
|
| 187 |
with gr.Column():
|
| 188 |
text_input = gr.Textbox(
|
| 189 |
placeholder="Paste syllabus or topic",
|
|
|
|
| 206 |
btn = gr.Button("Generate")
|
| 207 |
|
| 208 |
with gr.Tabs():
|
|
|
|
| 209 |
with gr.TabItem("Lesson Plan"):
|
| 210 |
lesson = gr.Markdown()
|
| 211 |
|