Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain_huggingface.llms import HuggingFacePipeline
|
| 3 |
+
from langchain_core.prompts import PromptTemplate
|
| 4 |
+
|
| 5 |
+
# Load the HuggingFace model
|
| 6 |
+
hf = HuggingFacePipeline.from_model_id(
|
| 7 |
+
model_id="gpt2",
|
| 8 |
+
task="text-generation",
|
| 9 |
+
pipeline_kwargs={"max_new_tokens": 10},
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Create a prompt template
|
| 13 |
+
template = """Question: {question}
|
| 14 |
+
|
| 15 |
+
Answer: Let's think step by step."""
|
| 16 |
+
prompt = PromptTemplate.from_template(template)
|
| 17 |
+
|
| 18 |
+
# Combine prompt with HuggingFace model
|
| 19 |
+
chain = prompt | hf
|
| 20 |
+
|
| 21 |
+
# Define a function for Gradio interface
|
| 22 |
+
def respond(question):
|
| 23 |
+
return chain.invoke({"question": question})
|
| 24 |
+
|
| 25 |
+
# Create Gradio ChatInterface
|
| 26 |
+
chat_interface = gr.ChatInterface(fn=respond, title="Q&A Chatbot", description="Ask any question and get an answer!")
|
| 27 |
+
|
| 28 |
+
# Launch the interface
|
| 29 |
+
chat_interface.launch()
|