| |
| import shutil |
| import gradio as gr |
| import os |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
|
| |
| model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-j-6b") |
| tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6b") |
|
|
|
|
| |
| cache_dir = os.path.join(os.getenv('TRANSFORMERS_CACHE', './cache')) |
|
|
| |
| shutil.rmtree(cache_dir, ignore_errors=True) |
|
|
| |
| |
| |
| qa_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
| |
| def answer_question(context, question): |
| |
| if not context.strip(): |
| return "Te rog să introduci un fragment de text." |
| |
| |
| if not question.strip(): |
| return "Te rog să introduci o întrebare." |
| |
| result = qa_pipeline({ |
| "context": context, |
| "question": question |
| }) |
| return result['answer'] |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Text Fragment Analyzer") |
| |
| |
| context = gr.Textbox(label="Fragmente de text", lines=10, placeholder="Introdu textul aici...") |
| question = gr.Textbox(label="Întrebare", placeholder="Ce vrei să întrebi despre text?") |
| |
| |
| answer = gr.Textbox(label="Răspuns", interactive=False) |
| |
| |
| btn = gr.Button("Generează răspuns") |
| |
| |
| btn.click(answer_question, inputs=[context, question], outputs=answer) |
|
|
| |
| demo.launch(share=True) |