Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
|
|
|
|
| 3 |
from langchain_community.llms import HuggingFacePipeline
|
| 4 |
-
from
|
| 5 |
from langchain.chains import LLMChain
|
| 6 |
|
| 7 |
# -------------------------
|
| 8 |
-
# Model
|
| 9 |
# -------------------------
|
| 10 |
MODEL_NAME = "google/flan-t5-base"
|
| 11 |
|
|
@@ -13,7 +15,7 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
|
| 13 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 14 |
|
| 15 |
hf_pipeline = pipeline(
|
| 16 |
-
|
| 17 |
model=model,
|
| 18 |
tokenizer=tokenizer,
|
| 19 |
max_length=256,
|
|
@@ -29,7 +31,7 @@ prompt = PromptTemplate(
|
|
| 29 |
input_variables=["question"],
|
| 30 |
template="""
|
| 31 |
You are an intelligent AI assistant.
|
| 32 |
-
Answer
|
| 33 |
|
| 34 |
Question: {question}
|
| 35 |
Answer:
|
|
@@ -51,10 +53,11 @@ def chat_fn(user_input):
|
|
| 51 |
# -------------------------
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=chat_fn,
|
| 54 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask your question
|
| 55 |
outputs="text",
|
| 56 |
title="LangChain + Hugging Face Chatbot",
|
| 57 |
-
description="
|
| 58 |
)
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 3 |
+
|
| 4 |
+
# ✅ UPDATED imports
|
| 5 |
from langchain_community.llms import HuggingFacePipeline
|
| 6 |
+
from langchain_core.prompts import PromptTemplate
|
| 7 |
from langchain.chains import LLMChain
|
| 8 |
|
| 9 |
# -------------------------
|
| 10 |
+
# Model (CPU safe)
|
| 11 |
# -------------------------
|
| 12 |
MODEL_NAME = "google/flan-t5-base"
|
| 13 |
|
|
|
|
| 15 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 16 |
|
| 17 |
hf_pipeline = pipeline(
|
| 18 |
+
"text2text-generation",
|
| 19 |
model=model,
|
| 20 |
tokenizer=tokenizer,
|
| 21 |
max_length=256,
|
|
|
|
| 31 |
input_variables=["question"],
|
| 32 |
template="""
|
| 33 |
You are an intelligent AI assistant.
|
| 34 |
+
Answer the question clearly.
|
| 35 |
|
| 36 |
Question: {question}
|
| 37 |
Answer:
|
|
|
|
| 53 |
# -------------------------
|
| 54 |
demo = gr.Interface(
|
| 55 |
fn=chat_fn,
|
| 56 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask your question..."),
|
| 57 |
outputs="text",
|
| 58 |
title="LangChain + Hugging Face Chatbot",
|
| 59 |
+
description="Running on Hugging Face Spaces (LangChain v0.1+ compatible)"
|
| 60 |
)
|
| 61 |
|
| 62 |
demo.launch()
|
| 63 |
+
|