Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,80 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from langchain_core.prompts import PromptTemplate
|
| 7 |
from langchain.chains import LLMChain
|
|
|
|
| 8 |
|
| 9 |
-
# -------------------------
|
| 10 |
-
#
|
| 11 |
-
# -------------------------
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
tokenizer=tokenizer,
|
| 21 |
-
max_length=256,
|
| 22 |
-
temperature=0.3
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
# Prompt
|
| 29 |
-
# -------------------------
|
| 30 |
prompt = PromptTemplate(
|
| 31 |
input_variables=["question"],
|
| 32 |
template="""
|
| 33 |
-
You are
|
| 34 |
-
Answer
|
| 35 |
|
| 36 |
Question: {question}
|
| 37 |
Answer:
|
| 38 |
"""
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# -------------------------
|
| 44 |
-
# Chat
|
| 45 |
-
# -------------------------
|
| 46 |
-
def
|
| 47 |
if not user_input.strip():
|
| 48 |
-
return "Please enter a
|
| 49 |
return chain.run(user_input)
|
| 50 |
|
| 51 |
-
# -------------------------
|
| 52 |
# Gradio UI
|
| 53 |
-
# -------------------------
|
| 54 |
demo = gr.Interface(
|
| 55 |
-
fn=
|
| 56 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask
|
| 57 |
outputs="text",
|
| 58 |
-
title="LangChain +
|
| 59 |
-
description="
|
| 60 |
)
|
| 61 |
|
| 62 |
demo.launch()
|
| 63 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
# -----------------------------
|
| 4 |
+
# LlamaIndex (LLM Layer)
|
| 5 |
+
# -----------------------------
|
| 6 |
+
from llama_index.llms.huggingface import HuggingFaceLLM
|
| 7 |
+
|
| 8 |
+
llm = HuggingFaceLLM(
|
| 9 |
+
model_name="google/flan-t5-base",
|
| 10 |
+
tokenizer_name="google/flan-t5-base",
|
| 11 |
+
context_window=512,
|
| 12 |
+
max_new_tokens=256,
|
| 13 |
+
generate_kwargs={
|
| 14 |
+
"temperature": 0.3,
|
| 15 |
+
"do_sample": False
|
| 16 |
+
}
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# LangChain (Prompt + Chain)
|
| 21 |
+
# -----------------------------
|
| 22 |
from langchain_core.prompts import PromptTemplate
|
| 23 |
from langchain.chains import LLMChain
|
| 24 |
+
from langchain.llms.base import LLM
|
| 25 |
|
| 26 |
+
# -----------------------------
|
| 27 |
+
# Adapter: LlamaIndex → LangChain
|
| 28 |
+
# -----------------------------
|
| 29 |
+
class LlamaIndexLLMAdapter(LLM):
|
| 30 |
+
"""Adapter to use LlamaIndex LLM inside LangChain"""
|
| 31 |
|
| 32 |
+
@property
|
| 33 |
+
def _llm_type(self):
|
| 34 |
+
return "llamaindex-huggingface"
|
| 35 |
|
| 36 |
+
def _call(self, prompt: str, stop=None):
|
| 37 |
+
response = llm.complete(prompt)
|
| 38 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Create LangChain-compatible LLM
|
| 41 |
+
langchain_llm = LlamaIndexLLMAdapter()
|
| 42 |
|
| 43 |
+
# Prompt Template
|
|
|
|
|
|
|
| 44 |
prompt = PromptTemplate(
|
| 45 |
input_variables=["question"],
|
| 46 |
template="""
|
| 47 |
+
You are a helpful AI assistant.
|
| 48 |
+
Answer clearly and concisely.
|
| 49 |
|
| 50 |
Question: {question}
|
| 51 |
Answer:
|
| 52 |
"""
|
| 53 |
)
|
| 54 |
|
| 55 |
+
# LangChain Chain
|
| 56 |
+
chain = LLMChain(
|
| 57 |
+
llm=langchain_llm,
|
| 58 |
+
prompt=prompt
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
# -----------------------------
|
| 62 |
+
# Chat Function
|
| 63 |
+
# -----------------------------
|
| 64 |
+
def chat(user_input):
|
| 65 |
if not user_input.strip():
|
| 66 |
+
return "Please enter a message."
|
| 67 |
return chain.run(user_input)
|
| 68 |
|
| 69 |
+
# -----------------------------
|
| 70 |
# Gradio UI
|
| 71 |
+
# -----------------------------
|
| 72 |
demo = gr.Interface(
|
| 73 |
+
fn=chat,
|
| 74 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask something..."),
|
| 75 |
outputs="text",
|
| 76 |
+
title="LangChain + LlamaIndex Chatbot",
|
| 77 |
+
description="Integrated chatbot (No RAG, No Vector DB)"
|
| 78 |
)
|
| 79 |
|
| 80 |
demo.launch()
|
|
|