File size: 1,033 Bytes
21d2028
8167236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21d2028
8167236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import spaces
import gradio as gr
from langchain_huggingface.llms import HuggingFacePipeline
from langchain.prompts import PromptTemplate

# Initialize the model pipeline
llm = HuggingFacePipeline.from_model_id(
    model_id="ibm-granite/granite-3.2-2b-instruct",
    task="text-generation",
    pipeline_kwargs={
        "max_new_tokens": 512,
    },
)

prompt = PromptTemplate(
    input_variables=["question"],
    template="Answer the following question: {question}",
)

# Create a chain with the model pipeline and prompt template
chain = prompt | llm

@spaces.GPU
def ask_question(question: str) -> str:
    # Invoke your chain and return the generated answer.
    response = chain.invoke({"question": question})
    return response

# Create a Gradio interface
iface = gr.Interface(
    fn=ask_question,
    inputs="text",
    outputs="text",
    title="Question Answering Bot",
    description="Ask any question and get an answer from the model."
)

# Launch the Gradio interface
if __name__ == "__main__":
    iface.launch()