This repository contains a simple Flask web application that serves as an API for text generation using the Llama 2 model. The application uses the Hugging Face Transformers library and enables Cross-Origin Resource Sharing (CORS) to allow requests from different domains. Additionally, it includes a ReactJS component that provides a user-friendly interface for interacting with the Flask API.
-
Install the required packages by running:
pip install flask transformers flask-cors
-
Make sure to have the Hugging Face Transformers library installed. You can install it using:
pip install transformers
-
Run the Flask application:
python your_app_name.py
-
Send a POST request to the
/generate_text
endpoint with a JSON payload containing theinput_prompt
. For example:curl -X POST -H "Content-Type: application/json" -d '{"input_prompt": "Once upon a time"}' http://localhost:5000/generate_text
-
The API will respond with the generated text as JSON:
{"generated_text": "Once upon a time, in a land far, far away..."}
/generate_text
(POST):-
Input:
-
JSON payload with the key
input_prompt
containing the text prompt for text generation.{"input_prompt": "Once upon a time"}
-
-
Output:
-
JSON response with the key
generated_text
containing the generated text.{"generated_text": "Once upon a time, in a land far, far away..."}
-
-
To integrate this Flask API with a ReactJS frontend, follow the steps below:
-
Install required dependencies in your React project:
npm install
-
Create a new component or modify an existing one. For example, create a file named
YourComponent.js
and use the provided React component. -
Import and use the
YourComponent
in your main application file (e.g.,App.js
). -
Style your component by creating a CSS file (e.g.,
YourComponent.css
) and importing it into your React component. -
Run your React application:
npm start
Now, your React component should interact with the Flask Text Generation API. The input prompt is sent to the API, and the generated text is displayed in the response box. Feel free to customize the React component and styles according to your project requirements.