ChavanN commited on
Commit
4dfc9ab
Β·
verified Β·
1 Parent(s): 1b3c803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -27
app.py CHANGED
@@ -1,36 +1,28 @@
1
  import gradio as gr
2
- from ctransformers import AutoModelForCausalLM
3
 
4
- # Load GGUF model directly from Hugging Face (no need to pre-download)
5
- model = AutoModelForCausalLM.from_pretrained(
6
- "TheBloke/Mistral-7B-Instruct-v0.1-GGUF", # repo_id
7
- model_file="mistral-7b-instruct-v0.1.Q4_K_M.gguf", # exact filename
8
- model_type="mistral", # required
9
- max_new_tokens=2048,
10
- temperature=0.9,
11
- repetition_penalty=1.1,
12
- top_p=0.95
13
- )
14
-
15
- # Define the Q&A generation function
16
- def generate_qa(text):
17
- prompt = f"""### Instruction:
18
- Based on the following SAP Note, generate exactly 20 unique and informative question-answer pairs.
19
- Each question must refer to the SAP note number from text if additional context is needed.
20
 
21
- ### Input:
22
- {text}
23
 
24
- ### Response:"""
25
- return model(prompt).strip()
 
 
 
 
 
 
26
 
27
- # Create Gradio UI
28
  demo = gr.Interface(
29
- fn=generate_qa,
30
- inputs=gr.Textbox(lines=20, label="SAP Note Text"),
31
- outputs=gr.Textbox(lines=30, label="Generated Q&A Pairs"),
32
- title="SAP Note Q&A Generator (Mistral GGUF - CPU)",
33
- description="Paste SAP Note content to generate 20 Q&A pairs using Mistral 7B (Quantized GGUF model)"
34
  )
35
 
36
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
3
 
4
+ # Load tokenizer and model (small T5 variant, CPU only)
5
+ model_name = "t5-small"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
8
+ model = T5ForConditionalGeneration.from_pretrained(model_name)
9
 
10
+ def generate_text(input_text):
11
+ # Prepare input tokens
12
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
13
+ # Generate output tokens (max length 100)
14
+ outputs = model.generate(input_ids, max_length=100, num_beams=5, early_stopping=True)
15
+ # Decode to text
16
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
17
+ return result
18
 
19
+ # Build Gradio interface
20
  demo = gr.Interface(
21
+ fn=generate_text,
22
+ inputs=gr.Textbox(lines=5, label="Input Text"),
23
+ outputs=gr.Textbox(lines=5, label="Generated Text"),
24
+ title="Simple T5 Text Generator (CPU)",
25
+ description="Enter some text, T5 will generate a continuation or answer."
26
  )
27
 
28
  if __name__ == "__main__":