Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 2 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
+
from langchain_huggingface import HuggingFacePipeline
|
| 4 |
+
from langchain_core.runnables import RunnableSequence
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model_id = "google/gemma-2b"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 11 |
+
|
| 12 |
+
# Text generation pipeline
|
| 13 |
+
generator = pipeline(
|
| 14 |
+
"text-generation",
|
| 15 |
+
model=model,
|
| 16 |
+
tokenizer=tokenizer,
|
| 17 |
+
max_new_tokens=200,
|
| 18 |
+
do_sample=True,
|
| 19 |
+
temperature=0.7
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# LangChain wrapper
|
| 23 |
+
llm = HuggingFacePipeline(pipeline=generator)
|
| 24 |
+
|
| 25 |
+
# Prompt template
|
| 26 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 27 |
+
("system", "You are a helpful assistant. Please respond to the user queries."),
|
| 28 |
+
("user", "Question: {question}")
|
| 29 |
+
])
|
| 30 |
+
|
| 31 |
+
# Runnable sequence instead of LLMChain
|
| 32 |
+
chain = prompt | llm
|
| 33 |
+
|
| 34 |
+
# Gradio interface
|
| 35 |
+
def generate_answer(question):
|
| 36 |
+
result = chain.invoke({"question": question})
|
| 37 |
+
return result
|
| 38 |
+
|
| 39 |
+
gr.Interface(fn=generate_answer, inputs="text", outputs="text", title="Gemma 2B Chat").launch()
|