Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,20 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
from llama_cpp import Llama
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
MODEL_FILE = "TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
|
| 9 |
|
| 10 |
-
# Download and cache model
|
| 11 |
-
try:
|
| 12 |
-
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE, cache_dir="./models")
|
| 13 |
-
except Exception as e:
|
| 14 |
-
raise RuntimeError(f"Failed to download model: {e}") from e
|
| 15 |
-
|
| 16 |
-
# Load with llama-cpp
|
| 17 |
-
llm = Llama(
|
| 18 |
-
model_path=model_path,
|
| 19 |
-
n_ctx=2048,
|
| 20 |
-
n_threads=4,
|
| 21 |
-
use_mlock=True
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# Answer function
|
| 25 |
def answer_question(question):
|
| 26 |
-
prompt = f"
|
| 27 |
-
|
| 28 |
-
return
|
| 29 |
|
| 30 |
-
# Gradio app
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=answer_question,
|
| 33 |
inputs=gr.Textbox(lines=2, label="Ask a programming question"),
|
| 34 |
outputs=gr.Textbox(label="Answer"),
|
| 35 |
-
title="
|
| 36 |
-
description="
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Load lightweight Flan-T5 model
|
| 5 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def answer_question(question):
|
| 8 |
+
prompt = f"Answer the following programming question clearly:\n{question}"
|
| 9 |
+
result = qa_pipeline(prompt, max_length=200, do_sample=True, top_p=0.9, temperature=0.7)[0]["generated_text"]
|
| 10 |
+
return result.strip()
|
| 11 |
|
|
|
|
| 12 |
demo = gr.Interface(
|
| 13 |
fn=answer_question,
|
| 14 |
inputs=gr.Textbox(lines=2, label="Ask a programming question"),
|
| 15 |
outputs=gr.Textbox(label="Answer"),
|
| 16 |
+
title="Lightweight Code Q&A (Flan-T5)",
|
| 17 |
+
description="Ask coding questions and get short, helpful answers using the Flan-T5 model."
|
| 18 |
)
|
| 19 |
|
| 20 |
if __name__ == "__main__":
|