yasserrmd commited on
Commit
1c77cbb
·
verified ·
1 Parent(s): 504e2dd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load the SmolLM-135M model and tokenizer
6
+ model_name = "HuggingFaceTB/SmolLM-135M-Instruct"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ def generate_feedback(text):
11
+ # Generate a prompt for writing feedback
12
+ prompt = f"Provide constructive feedback on the following creative writing piece:\n\n{text}\n\nFeedback:"
13
+
14
+ # Tokenize the input
15
+ inputs = tokenizer(prompt, return_tensors="pt")
16
+
17
+ # Generate the response
18
+ with torch.no_grad():
19
+ outputs = model.generate(inputs.input_ids, max_length=150, do_sample=True, top_p=0.85, temperature=0.7)
20
+
21
+ # Decode the response
22
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
23
+ feedback = response.split("Feedback:")[-1].strip() # Extract feedback portion
24
+ return feedback
25
+
26
+ # Set up the Gradio interface
27
+ app = gr.Interface(
28
+ fn=generate_feedback,
29
+ inputs=gr.inputs.Textbox(lines=5, label="Your Writing", placeholder="Paste a short piece of creative writing here..."),
30
+ outputs="text",
31
+ title="Micro-Feedback Assistant for Creative Writing",
32
+ description="Get quick, constructive feedback on your creative writing. Ideal for short stories, poetry, or essays. "
33
+ "Feedback may cover tone, grammar, and word choice."
34
+ )
35
+
36
+ # Launch the app
37
+ app.launch()