SHAILJA1 commited on
Commit
da09273
·
verified ·
1 Parent(s): db1afc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -35
app.py CHANGED
@@ -1,63 +1,80 @@
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
 
14
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
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,
22
- temperature=0.3
23
- )
24
 
25
- llm = HuggingFacePipeline(pipeline=hf_pipeline)
 
26
 
27
- # -------------------------
28
- # Prompt
29
- # -------------------------
30
  prompt = PromptTemplate(
31
  input_variables=["question"],
32
  template="""
33
- You are an intelligent AI assistant.
34
- Answer the question clearly.
35
 
36
  Question: {question}
37
  Answer:
38
  """
39
  )
40
 
41
- chain = LLMChain(llm=llm, prompt=prompt)
 
 
 
 
42
 
43
- # -------------------------
44
- # Chat function
45
- # -------------------------
46
- def chat_fn(user_input):
47
  if not user_input.strip():
48
- return "Please enter a question."
49
  return chain.run(user_input)
50
 
51
- # -------------------------
52
  # Gradio UI
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
-
 
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()